This question already has an answer here:
I have to upload multiple images into MySQL, but this code can not get the proper output. How can I fix this?
This is my code:
$target_folder = "../tour-images/";
if ($_FILES['photo_url']['name']) {
//echo "hii";
$allowable_extensions = array("image/jpg", "image/jpeg", "image/pjpeg", "image/png", "image/gif");
$imagename = $_FILES["photo_url"]["name"];
//echo "<br>hii".$imagename."<br>".$allowable_extensions;
$file_type = $_FILES["photo_url"]["type"];
print_r($file_type);
if (in_array($file_type, $allowable_extensions)) {
for($i = 0; $i < count($_FILES['file']['name']); $i++) {
//$file_ext = substr($file_type, 6);
$file_ext = explode('.', $file_type[$i]);
echo $file_ext;
$source = $_FILES["photo_url"]['tmp_name'];
$target1 = $target_folder . $imagename . "." . $file_ext;
echo "<br>" . $target1;
$photourl = $target_folder . $imagename . "." . $file_ext;
echo "hcxxxxxxx" . $photourl;
if (move_uploaded_file($source, $target1))
{
$filename11 = $target1;
}
}
}
}
else {
$filename = $target_folder . "noLogo.jpeg";
$photourl = "noLogo.jpeg";
}
$sql = "INSERT INTO image_details(tour_id,photo_url) VALUES('".$id."', '".$filename11."')";
mysql_query($sql);
echo $sql;
</div>
Try the following code to upload multiple files and store in the database.
<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="text" name="Filename">
<input type="file" name="photo_url[]" multiple>
<input type="submit" value="Upload">
</form>
<?php
if (isset($_FILES['photo_url'])) {
$images = array();
$myFile = $_FILES['photo_url'];
$fileCount = count($myFile["name"]);
$Filename=$_POST['Filename'];
$structure = "Uploads/SystemConfiguration/$Filename";
if (!mkdir($structure, 0777, true))
{
die('Failed to create folders...');
}
for ($i = 0; $i < $fileCount; $i++) {
$name = $_FILES["photo_url"]["name"][$i];
$tmp_name=$_FILES["photo_url"]["tmp_name"][$i];
move_uploaded_file($tmp_name, "$structure/$name");
$images[] = "$structure/$name";
}
}
$all_images = implode(",",$images);
?>
</body>
</html>
$sql = "INSERT INTO image_details(tour_id,photo_url) VALUES('".$id."','".$all_images."')";
mysql_query($sql);
echo $sql;