Multiple Image Upload PHP form with one input
$error=array();
$extension=array("jpeg","jpg","png","gif");
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name) {
$file_name=$_FILES["files"]["name"][$key];
$file_tmp=$_FILES["files"]["tmp_name"][$key];
$ext=pathinfo($file_name,PATHINFO_EXTENSION);
if(in_array($ext,$extension)) {
if(!file_exists("photo_gallery/".$txtGalleryName."/".$file_name)) {
move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$file_name);
}
else {
$filename=basename($file_name,$ext);
$newFileName=$filename.time().".".$ext;
move_uploaded_file($file_tmp=$_FILES["files"]["tmp_name"][$key],"photo_gallery/".$txtGalleryName."/".$newFileName);
}
}
else {
array_push($error,"$file_name, ");
}
}
and you must check your HTML code
<form action="create_photo_gallery.php" method="post" enctype="multipart/form-data">
<table width="100%">
<tr>
<td>Select Photo (one or multiple):</td>
<td><input type="file" name="files[]" multiple/></td>
</tr>
<tr>
<td colspan="2" align="center">Note: Supported image format: .jpeg, .jpg, .png, .gif</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Create Gallery" id="selectedButton"/></td>
</tr>
</table>
</form>
Nice link on:
PHP Single File Uploading with vary basic explanation.
PHP file uploading with the Validation
PHP Multiple Files Upload With Validation Click here to download source code
PHP/jQuery Multiple Files Upload With The ProgressBar And Validation (Click here to download source code)
How To Upload Files In PHP And Store In MySql Database (Click here to download source code)
Uploading multiple images with one input field
You are probably looking for something like uploadify or swfupload or plupload.
Uploading multiple images and text input with php
Without going into code in great detail, here is generally what you are doing wrong and how you should do it.
The global $_FILES will contain all the uploaded file information. Its contents from the example form is as follows.
Array
(
[img] => Array
(
[name] => Array
(
[0] => bears.jpeg
[1] => big cat.jpeg
[2] => butterfly2.jpeg
[3] => chipmunk.jpeg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
[3] => image/jpeg
)
[tmp_name] => Array
(
[0] => /tmp/phpNKGKa2
[1] => /tmp/phpOCopiT
[2] => /tmp/phphEGfqK
[3] => /tmp/phpguKfyB
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
[size] => Array
(
[0] => 162804
[1] => 133032
[2] => 118203
[3] => 164941
)
)
)
When you upload files, on the PHP side you will get a structure something like this:
So you have to walk through this structure to get all the files. The form data on the other hand is only stored once in $_POST
.
So you must insert the form data once and the files you must use a loop to go through them all.
// INSERT form data first outside the loop
// Then go through the files in a loop
foreach ($_FILES["img"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
// INSERT file here
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = basename($_FILES["pictures"]["name"][$key]);
// Usually you have to do this
move_uploaded_file($tmp_name, "some/real/dir");
}
}
How to upload multiple files from HTML form, using PHP SWITCH function with more than one input file?
Did you try printing the content of $_FILES? It would clarify things for you.
No matter how many input files do you have, or how you named it.
Just iterate $_FILES.
if(!empty($_FILES))
{
foreach($_FILES as $file)
{
$name = $file['name'];
$folder = "/uploads";
move_uploaded_file($name, $folder . "/" . $name);
}
}
Added a full HTML/PHP example as OP needed.
<form method="post" enctype="multipart/form-data">
<input type="file" name="file1">
<input type="file" name="file2">
<input type="file" name="file3">
<input type="submit">
</form>
<?php
if(!empty($_FILES))
{
foreach($_FILES as $file)
{
$name = basename($file['name']);
$folder = "/uploads";
move_uploaded_file($name, $folder . "/" . $name);
}
}
?>
In this case, $_FILES gives you something like this structure:
Array
(
[file1] => Array
(
[name] => IMG_20180918_094315304.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpYDmeY7
[error] => 0
[size] => 5342893
)
[file2] => Array
(
[name] => IMG_20180918_094323718.jpg
[type] => image/jpeg
[tmp_name] => /tmp/php9cXCkE
[error] => 0
[size] => 5783548
)
[file3] => Array
(
[name] => IMG_20180918_094336974.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phphljHJa
[error] => 0
[size] => 4819618
)
)
Upload Multiple Images with PHP and put it into MYSQL Database
Slug is saving as an array, because it is an array.
$slug = $_POST['slug'];
should solve your problem, assuming the slug elements are added in the same order as your files.
As per the comments, the above PHP has been fixed and the fixed HTML below - all I did was remove the array directive from the input:
<form method="POST" action="action-add-images.php" enctype="multipart/form-data">
<input type="hidden" name="slug" value="<?php echo $_GET['slug']; ?>">
<label>Upload Files</label>
<input required type="file" name="image[]" class="form-control-file" multiple>
<button type="submit" class="btn btn-block btn-primary my-3 ">Upload Images</button>
Related Topics
How Can Strip Whitespaces in PHP'S Variable
How to Get Random Value Out of an Array
PHP Algorithm to Generate All Combinations of a Specific Size from a Single Set
How to Loop Through a MySQL Result Set
Simple Postgresql Statement - Column Name Does Not Exists
Can HTML Be Embedded Inside PHP "If" Statement
Why Are My Cookies Not Setting
How to Attach Two or Multiple Files and Send Mail in PHP
MySQLi Query Returns Only One Row
Right Way to Escape Backslash [ \ ] in PHP Regex
Is Possible to Keep Session Even After the Browser Is Closed
PHP Login System: Remember Me (Persistent Cookie)
Disable PHP in Directory (Including All Sub-Directories) With .Htaccess