1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Creating a Multi-File Upload Script in PHP

Discussion in 'PHP' started by Sanskruti, Feb 20, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    17
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    File upload is very common requirement of many web sites. We may require to upload pictures online to websites. Image upload is very common requirement and many sites are using this to allow members or visitors to upload files.

    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.

    Multiple files can be uploaded using different name for input.

    It is also possible to upload multiple files simultaneously and have the information organized automatically in arrays for you. To do so, you need to use the same array submission syntax in the HTML form as you do with multiple selects and checkboxes:

    Support for multiple file uploads was added in PHP 3.0.10.
    HTML:
    <form action="file-upload.php" method="post" enctype="multipart/form-data">
      Send these files:<br />
      <input name="userfile[]" type="file" /><br />
      <input name="userfile[]" type="file" /><br />
      <input type="submit" value="Send files" />
    </form>
    When the above form is submitted, the arrays $_FILES['userfile'], $_FILES['userfile']['name'], and $_FILES['userfile']['size'] will be initialized.. When register_global is on, globals for uploaded files are also initialized. Each of these will be a numerically indexed array of the appropriate values for the submitted files.

    register_globals tells whether or not to register the EGPCS (Environment, GET, POST, Cookie, Server) variables as global variables.

    Let us see one more example.
    PHP:
    $max_no_img=5// 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='image[]' 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 addimg.php file. The file addimg.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 upimage )
    PHP:
    while(list($key,$value) = each($_FILES[image][name]))
    {
        if(!empty(
    $value)) // this will check if any blank field is entered
        
    {   
            
    $filename $value;  // filename stores the value
            
    $add "upimage/$filename";  // upload directory path is set
            //echo $_FILES[image][type][$key];  // uncomment this line if you want to display the file type
            // echo "<br>"; // Display a line break
            
    copy($_FILES[image][tmp_name][$key], $add); //  upload the file to the server
            
    chmod("$add",0777);  // set permission to the file. 
        
    }
    }
    As this script allows more than one file to upload so this script is likely to take more time than normal execution time.
     
    nilesh@yudiz likes this.
  2. khan27

    khan27 New Member

    Joined:
    Sep 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    This fucntion is very useful to me. thanks for given such type of soluation.
     
  3. sneha123

    sneha123 New Member

    Joined:
    Dec 8, 2010
    Messages:
    17
    Likes Received:
    0
    Trophy Points:
    0
    Hi friends,,,Excellent post,,,,I think its nice information of Creating a Multi-File Upload Script in PHP.....Thanks for the share valuable solution here, it will be very helpful for all people……….:crazy::crazy:.
     
  4. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    I have to learn much of PHP i think, this helped me and I learnt something new, thx for that!
     
  5. nilesh@yudiz

    nilesh@yudiz New Member

    Joined:
    Oct 8, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
  6. laki1412

    laki1412 New Member

    Joined:
    Mar 20, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
  7. j.smith1981

    j.smith1981 New Member

    Joined:
    Mar 22, 2012
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    Can't say anything more than a great post indeed!
     
  8. j.smith1981

    j.smith1981 New Member

    Joined:
    Mar 22, 2012
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    UK
    Just wanted to say about this post here:
    I felt exactly like you did some time ago but keep sticking at coding it'll make sense soon trust me!
     
  9. steven_elvisda

    steven_elvisda New Member

    Joined:
    Dec 5, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    It is really awesome. current I limited 5 images and need to 5 clicks to completed.
     

Share This Page