SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP File Function

PHP multiple file upload Script

We will learn how to upload multiple files using a single form. This is required if you are allowing members to upload more than one file and you don't know how many files you allow them to upload. Say you want up to 8 files any member of your site can upload. As your members upload and delete files so at the time of displaying the form to allow them to upload you would like to check the existing number of files they have and accordingly display them upload fields to add files. Here will try that.  Here we will set one variable to the number of text fields to display and this value can be controlled based on the requirement or can be set by a script. Here is the code to display the form and please read the file upload tutorial on this section.

$max_no_img=4; // Maximum number of images value to be set here

echo "<form method=post action=addimgck.php enctype='multipart/form-data'>";
echo "<table border='0' width='400' cellspacing='0' cellpadding='0' align=center>";
for($i=1; $i<=$max_no_img; $i++){
echo "<tr><td>Images $i</td><td>
<input type=file name='images[]' class='bginput'></td></tr>";
}

echo "<tr><td colspan=2 align=center><input type=submit value='Add Image'></td></tr>";
echo "</form> </table>";


This part will display the form and the number of upload boxes as per set by the variable. Now we will move to next part explaining how to handle the uploaded file and how to know what are the fields uploaded by the user. We will be using PHP  Multidimensional array here. We will receive the field data in the addimgck.php file. The file addimgck.php  will use php multidimensional array and we will use array display techniques to know the input fields for file upload. Please ensure that write permission is given to the directory where files are to be stored. ( Here the directory name is upimg )

while(list($key,$value) = each($_FILES[images][name]))
{
if(!empty($value)){   // this will check if any blank field is entered
$filename = $value;    // filename stores the value

$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in name, you can remove this line

$add = "upimg/$filename";   // upload directory path is set
//echo $_FILES[images][type][$key];     // uncomment this line if you want to display the file type
// echo "<br>";                             // Display a line break
copy($_FILES[images][tmp_name][$key], $add);     //  upload the file to the server
chmod("$add",0777);                 // set permission to the file.
}
}

HTML5 multiple file upload

In HTML5 we have a better option to use. Here no need to show multiple input fields to upload multiple files. One single button will be displayed for the user. On click of the button user can browse their local file system and select more than one file by pressing ctrl or shift key.
We can use the html form part like this

<form action="uploadck.php" enctype="multipart/form-data" method="post">
<input type="file" multiple name="images[]" value="select files">
<input type=submit value='Upload Images'>
</form>

Note the attribute multiple inside upload tag. With this we can press ctrl key in our keyboard and select multiple file at same time. We can select files as many as we want. The same will be available for the backend PHP script. There is not much difference and we have not checked the number of files to be uploaded here.

<?Php
while(list($key,$value) = each($_FILES[images][name]))
{
$filename = $value; // filename stores the value

$filename=str_replace(" ","_",$filename);// Add _ inplace of blank space in name, you can remove this line

$add = "upimg/$filename"; // upload directory path is set
//echo $add; // uncomment this line if you want to display the file name with path
//echo "<br>";
//echo $_FILES[images][type][$key]; // uncomment this line if you want to display the file type
echo "<br>"; // Display a line break
copy($_FILES[images][tmp_name][$key], $add); // upload the file to the server
chmod("$add",0777); // set permission to the file.
}
?>

Using iframe to upload multiple files

We are submitting the form to a different page to execute the code for uploading files. Without taking the browser to uploadck.php we can execute the uploading code by keeping one iframe. We will hide the border and only display the output displayed by uploadck.php file within the iframe. The hidden iframe ( without border ) src parameter will execute uploadck.php file.

Here is the change in code to display the file uploading form with iframe.

<form action="uploadck.php" enctype="multipart/form-data" method="post" target='myiframe'>
<input type="file" multiple name="images[]" value="select files">
<input type=submit value='Upload Images'>
</form>

<iframe name='myiframe' src='uploadck.php' width="300" height="300" frameBorder="0">
</iframe>

As this script allows more than one file to upload so this script is likely to take more time than normal execution time. So it is better to set a higher value to manage maximum script execution time for this script.

Thumbnail Image generation from uploaded files

Php file upload


Download the ZIP file here  php_multi_file_upload.zip

Be the first to post comment on this article :


Google+
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer