Multiple Files Upload with PHP

In this tutorial we will make a multiple file Upload system with PHP. The system will automatically create directory if not previously created for storing uploaded files. This multiple file Upload system can be used to upload images, PDF's, Doc's or any file types.
You can also see a tutorial on Single File Upload and Jquery Ajax File Upload.

THE HTML

This is a simple HTML form containing interface for multiple file Upload system.
<html>
    <head>
        <title>Multiple Files Upload with PHP</title>
    </head>
    <body>
        <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
            <tr>
            <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
                <td>
                    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                        <tr>
                            <td><strong>Multiple Files Upload with PHP</strong></td>
                        </tr>
                        <tr>
                            <td>Select file 
                                <input name="userfile[]" type="file" id="userfile[]" size="50" /></td>
                        </tr>
                        <tr>
                            <td>Select file
                                <input name="userfile[]" type="file" id="userfile[]" size="50" /></td>
                        </tr>
                        <tr>
                            <td>Select file
                                <input name="userfile[]" type="file" id="userfile[]" size="50" /></td>
                        </tr>
                        <tr>
                            <td align="center"><input type="submit" name="Submit" value="Upload" /></td>
                        </tr>
                    </table>
                </td>
            </form>
        </tr>
    </table>
</body>
</html>
Make sure to make add enctype="multipart/form-data ,type="file" and most importantly name="userfile[]" to enable multi files selection possible.

PHP

Moving on to the PHP codes. we can get started with isset($_POST['Submit']), in this we are checking if the upload button is clicked or not. If upload button is clicked then we can move further for validating and uploading files. $_FILES[' '] will contain the information of each file, but when more that one file are selected it will have the details of each file enclosed inside another array.
To access them we will be using foreach loop.
foreach($_FILES['userfile']['tmp_name'] as $key => $tmp_name ){ $file_name = $key.$_FILES['userfile']['name'][$key]; $file_size =$_FILES['userfile']['size'][$key]; $file_tmp =$_FILES['userfile']['tmp_name'][$key]; $file_type=$_FILES['userfile']['type'][$key]; }
Now we will check the size of uploaded files. If the size of any file is equal to 0 means file is not selected for upload and we through an error.
if($file_size == 0){ $errors='There is something error in your files.'; }

MOVING THE FILES

File uploaded will be allocated space in the temporary location as mentioned in the php.ini. It is necessary to move the file from the temporary location to another in order to use it again. You can get the file moved to another location using move_uploaded_file(), here we will move it to an "Upload" folder, make sure the directory exists, since move_uploaded_file() cannot create a directory. So it recommended to verify for existence of directory.
$upload_dir="upload"; move_uploaded_file($file_tmp,$upload_dir."/".$file_name);
If you plan to create a directory on the go, then you can use mkdir(DIR NAME,PERMISION)
if($file_size == 0){ $errors='There is something error in your files.'; }

FULL CODE FOR MILTIPLE FILES UPLOAD WITH PHP

You can use the following codes to upload files of any size & type.
<?php
if(isset($_POST['Submit'])){
 foreach($_FILES['userfile']['tmp_name'] as $key => $tmp_name ){
  $file_name = $key.$_FILES['userfile']['name'][$key];
  $file_size =$_FILES['userfile']['size'][$key];
  $file_tmp =$_FILES['userfile']['tmp_name'][$key];
  $file_type=$_FILES['userfile']['type'][$key]; 
   
   if($file_size == 0){
    $errors='There is something error in your files.';
   }
   $upload_dir="upload";
   if(empty($errors)==true){
    if(is_dir($upload_dir)==false){
     mkdir("$upload_dir", 0700);  // Create directory if it does not exist
    } 
     move_uploaded_file($file_tmp, $upload_dir."/".$file_name);
   }
  }
 if(empty($errors)){
  echo "Your files uploaded successfully!!!";
 }else{
   print_r($errors);
 }
}
?>
<html>
    <head>
        <title>Multiple Files Upload with PHP</title>
    </head>
    <body>
        <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
            <tr>
            <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
                <td>
                    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                        <tr>
                            <td><strong>Multiple Files Upload with PHP</strong></td>
                        </tr>
                        <tr>
                            <td>Select file 
                                <input name="userfile[]" type="file" id="userfile[]" size="50" /></td>
                        </tr>
                        <tr>
                            <td>Select file
                                <input name="userfile[]" type="file" id="userfile[]" size="50" /></td>
                        </tr>
                        <tr>
                            <td>Select file
                                <input name="userfile[]" type="file" id="userfile[]" size="50" /></td>
                        </tr>
                        <tr>
                            <td align="center"><input type="submit" name="Submit" value="Upload" /></td>
                        </tr>
                    </table>
                </td>
            </form>
        </tr>
    </table>
</body>
</html>
Hope this php tutorial is useful for you. Keep following Php Point for more Codes.

No comments:

Post a Comment