Monday, April 7, 2014

PHP, upload multiple files and add progress bar



 The difference between upload one file and multiple files in PHP is that we put array in name in
input, example, for upload a single file
 <input type="file"  name="file_single">
for upload multiple files, add [] in name, example
<input type="file" name="file_array[]">
 Complete example for uploading multiple files in PHP:
main.html
<!DOCTYPE html> 
<html>
 <head>
 <meta charset="UTF-8">
 </head> 
<body> 
<form action="upload.php" method="post" enctype="multipart/form-data">
 <p><input type="file" name="file_array[]">
 <input type="submit" value="Upload all files">
 </form> 
</body>
 </html>

upload.php
<?php /
 if(isset($_FILES['file_array'])){
 $name_array = $_FILES['file_array']['name']; 
$tmp_name_array = $_FILES['file_array']['tmp_name']; 
 $type_array = $_FILES['file_array']['type'];
 $size_array = $_FILES['file_array']['size']; 
$error_array = $_FILES['file_array']['error']; 
for($i = 0; $i < count($tmp_name_array); $i++){
 if(move_uploaded_file($tmp_name_array[$i], "test_uploads/".$name_array[$i]))
{ echo $name_array[$i]." upload is complete<br>"; }
 else {
 echo "move_uploaded_file function failed for ".$name_array[$i]."<br>"; } }
 } ?>

About  add upload progress bar:
Video: File Upload Progress Bar Meter Tutorial HTML5 Ajax PHP


Reference:
http://www.developphp.com/view.php?tid=1351

No comments:

Post a Comment