How To Upload/store Multiple Images In Mysql Database In Php
Here I'm making a form in which user can upload multiple images HTML Code below PHP Code below $tmp_file = $_FILES[
Solution 1:
Use <input type='file' name='attachPhoto1[]' multiple />
.
Notice the square brackets (highlighting them did not work for me).
In PHP you should iterate over the members of the $attachPhoto1[] array.
Solution 2:
Your input should be like this, when you upload and posting mutiple image files
<input type='file' name='attachPhoto1[]' multiple />
and your form should be like this
<form action="" method="post" name="" id="" enctype="multipart/form-data">
and following is very much working code i used, you can adjust it according to your need;
if(!empty($_FILES['attachPhoto1']['name'])) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$error_uploads = 0;
$total_uploads = array();
$upload_path = 'upload/';
foreach($_FILES['attachPhoto1']['name'] as$key => $value) {
$temp = explode(".", $_FILES['attachPhoto1']['name'][$key]);
$extension = end($temp);
if ($_FILES["files"]["type"][$key] != "image/gif"
&& $_FILES["files"]["type"][$key] != "image/jpeg"
&& $_FILES["files"]["type"][$key] != "image/jpg"
&& $_FILES["files"]["type"][$key] != "image/pjpeg"
&& $_FILES["files"]["type"][$key] != "image/x-png"
&& $_FILES["files"]["type"][$key] != "image/png"
&& !in_array($extension, $allowedExts)) {
$error_uploads++;
continue;
}
$file_name = time().rand(1,5).rand(6,10).'_'.str_replace(' ', '_', $_FILES["attachPhoto1"]['name'][$key]);
if(move_uploaded_file($_FILES["attachPhoto1"]['tmp_name'][$key], $upload_path.$file_name)) {
$total_uploads[] = $file_name;
} else {
$error_uploads++;
}
}
if(sizeof($total_uploads)) {
//Do what ever you like after file uploads, you can run query here to save it in database or set redirection after success upload
}
}
}
Solution 3:
Refer this code for uploading more than one images in specified folder
<?phpif (isset($_POST['submit'])) {
$j = 0; // Variable for indexing uploaded image.$target_path = "uploads/"; // Declaring Path for uploaded images.// --- Loop to get individual element from the arrayfor ($i = 0; $i < count($_FILES['file']['name']); $i++) {
$validextensions = array("jpeg", "jpg", "png"); // Extensions which are allowed.$ext = explode('.', basename($_FILES['file']['name'][$i])); // Explode file name from dot(.)$file_extension = end($ext); // Store extensions in the variable.$target_path = $target_path . md5(uniqid()) . "." . $ext[count($ext) - 1]; // Set the target path with a new name of image.$j = $j + 1; // Increment the number of uploaded images according to the files in array.// --- Approx. 100kb files can be uploaded.if (($_FILES["file"]["size"][$i] < 100000) && in_array($file_extension, $validextensions)) {
if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {
// If file moved to uploads folder.echo$j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';
} else { // If File Was Not Moved.echo$j. ').<span id="error">please try again!.</span><br/><br/>';
}
} else { // If File Size And File Type Was Incorrect.echo$j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
}
}
}
?>
Post a Comment for "How To Upload/store Multiple Images In Mysql Database In Php"