Reputation: 165
I am currently very puzzled over why I am unable to insert an array of images over into my database. As of now, my current multiple file upload is able to upload images into my default directory, and able to store ONLY the first image into my SQL server database, why is this so? Shouldn't the foreach command be able to split all the multiple file that i upload and store them respectively into the database? Please shed some lights on this, thank you!
HTML Code
<form method="post" enctype="multipart/form-data" action="">
<input type="file" name="files[]" id="files" multiple />
<br /><br />
<button type="submit">Upload selected files</button>
PHP Code
foreach ($_FILES["files"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["files"]["name"][$key];
move_uploaded_file($_FILES["files"]["tmp_name"][$key], "" . $_FILES['files']['name'][$key]);
$sql = "INSERT INTO `test`(`image`) VALUES ('" . $name . "')";
$result = mysqli_query($connection, $sql);
echo "The file " . basename($_FILES['multiple_uploaded_files']['name']) . " has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}
}
Cheers, A tech newbie learning in progress.
Upvotes: 0
Views: 13331
Reputation: 83
try with this example code,
$path = "imageuploads/";
for($i=0; $i<count($_FILES['file']['name']); $i++){
$extension = explode('.', basename( $_FILES['file']['name'][$i]));
$path = $path . md5(uniqid()) . "." . $extension[count($extension)-1];
if(move_uploaded_file($_FILES['file']['tmp_name'][$i], $path )) {
//insert query
echo "uploaded successfully";
} else{
echo "Error in Upload";
}
}
Above code is not worked, please tell the scenario
Upvotes: 1