Multiple File Upload with PHP
Replace input file name like files[]
not like files
see following form example:
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
<input type="submit" value="Upload!" />
</form>
How to upload multiple files using PHP, jQuery and AJAX
Finally I have found the solution by using the following code:
$('body').on('click', '#upload', function(e){
e.preventDefault();
var formData = new FormData($(this).parents('form')[0]);
$.ajax({
url: 'upload.php',
type: 'POST',
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
return myXhr;
},
success: function (data) {
alert("Data Uploaded: "+data);
},
data: formData,
cache: false,
contentType: false,
processData: false
});
return false;
});
Upload multiple files using PHP
To allow multiple file uplodes you need to set attribute multiple="multiple"
on the file input with a name file[]
. Or you can use separate inputs with a name file[]
as well. Than to store values in database make a
foreach($_FILES['filename']['name'] as $key=>$name){
// store each file and write data to DB for each file
//for example to get the name of file
$name = $_FILES['filename']['name'][$key];
}
Uploading multiple files and renaming - PHP
You need a loop:
if(isset($_FILES['files'])){
$name_array = $_FILES['files']['name'];
$tmp_name_array = $_FILES['files']['tmp_name'];
// Number of files
$count_tmp_name_array = count($tmp_name_array);
// We define the static final name for uploaded files (in the loop we will add an number to the end)
$static_final_name = "name";
for($i = 0; $i < $count_tmp_name_array; $i++){
// Get extension of current file
$extension = pathinfo($name_array[$i] , PATHINFO_EXTENSION);
// Pay attention to $static_final_name
if(move_uploaded_file($tmp_name_array[$i], "uploads/".$static_final_name.$i.".".$extension)){
echo $name_array[$i]." upload is complete<br>";
} else {
echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
}
}
}
Upload Multiple Files In PHP
PHP
foreach($_FILES['image']['name'] as $i => $name)
{
// now $name holds the original file name
$tmp_name = $_FILES['image']['tmp_name'][$i];
$error = $_FILES['image']['error'][$i];
$size = $_FILES['image']['size'][$i];
$type = $_FILES['image']['type'][$i];
if($error === UPLOAD_ERR_OK)
{
$extension = getExtension($name);
if( ! in_array(strtolower($extension, array('jpg', 'jpeg', 'png') )
{
// Error, invalid extension detected
}
else if ($size > $maxAllowedFileSize /* needs to be defined elsewhere */)
{
// Error, file too large
}
else
{
// No errors
$newFileName = 'foo'; // You'll probably want something unique for each file.
if(move_uploaded_file($tmp_file, $newFileName))
{
// Uploaded file successfully moved to new location
$thumbName = 'thumb_image_name';
$thumb = make_thumb($newFileName, $thumbName, WIDTH, HEIGHT);
}
}
}
}
HTML
<form method="post" enctype="multipart/form-data">
<input name="image[]" type="file" class="image-upload" />
<input name="image[]" type="file" class="image-upload" />
<input name="image[]" type="file" class="image-upload" />
<input name="image[]" type="file" class="image-upload" />
<input name="image[]" type="file" class="image-upload" />
<!-- You need to add more, including a submit button -->
</form>
Note the name of the input elements. The []
at the end cause PHP to create an array and add the items to the array.
Make directory and upload multiple file into created directory PHP
mkdir
returns a boolean (true or false), not the created directory path.
You probably want to define the path in $upload_dir
but not assign the result of the mkdir to it:
$upload_dir = 'C:/fileUpload/'. $rand_name .'/';
if (mkdir($upload_dir, 0777)) {
//process images
}
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)
Related Topics
Access a JavaScript Variable from PHP
When Should I Use MySQLi Instead of MySQL
How to Use Password Hashing With Pdo to Make My Code More Secure
How to Use MySQLi Prepared Statements
Strange Character Encoding of Stored Data , Old Script Is Showing Them Fine New One Doesn'T
Slowness Found When Base 64 Image Select and Encode from Database
How to Convert a Pdf Document to a Preview Image in PHP
How to Properly Add Cross-site Request Forgery (Csrf) Token Using PHP
How to [Recursively] Zip a Directory in PHP
"Date(): It Is Not Safe to Rely on the System'S Timezone Settings..."
How to Repair a Serialized String Which Has Been Corrupted by an Incorrect Byte Count Length
Unicode Character in PHP String
How to Get the Cookies from a PHP Curl into a Variable
Return Single Column from a Multi-Dimensional Array
Fatal Error: Uncaught Error: Call to Undefined Function MySQL_Connect()
Is There a PHP Function That Can Escape Regex Patterns Before They Are Applied