Upload multiple files using php

11 replies
I have made code for uploading single file but i want to know how can i upload more than one file using php?

Is coding same or should I modify something for it?
#files #multiple #php #upload
  • Profile picture of the author skillboyz
    Here is a form
    HTML Code:
    <form action="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>
    <?php
    //places files into same dir as form resides
    foreach ($_FILES["userfile"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
    echo"$error_codes[$error]";
    move_uploaded_file(
    $_FILES["pictures"]["tmp_name"][$key],
    $_FILES["pictures"]["name"][$key]
    ) or die("Problems with upload");
    }
    }
    ?>
    {{ DiscussionBoard.errors[8274659].message }}
    • Profile picture of the author jasontrailor
      Uploading multiple files
      <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>
      {{ DiscussionBoard.errors[8274738].message }}
      • Profile picture of the author webprogramzone
        Originally Posted by jasontrailor View Post

        Uploading multiple files
        <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>
        I think you are missing something where is code of php? Can you tell me from where i will get it?
        {{ DiscussionBoard.errors[8274816].message }}
        • Profile picture of the author Brandon Tanner
          Here's a quick and dirty example...

          1) Code for your starting page (ie index.html, or whatever)...

          HTML Code:
          <!DOCTYPE html>  
          <html>  
          <head>
              <meta charset="utf-8">
              <title></title>  
          </head>  
          <body>  
              <form method="post" enctype="multipart/form-data"  action="upload.php">  
                  <input type="file" name="files[]" id="files" multiple />  
                  <br /><br />
                  <button type="submit">Upload selected files</button>  
              </form>  
          </body>  
          </html>
          2) Next you will need to create a page named upload.php in the same directory as your other page, and put the following code in it...

          <?php
          foreach ($_FILES["files"]["error"] as $key => $error) {
          if ($error == UPLOAD_ERR_OK) {
          $name = $_FILES["files"]["name"][$key];
          move_uploaded_file( $_FILES["files"]["tmp_name"][$key], "uploads/" . $_FILES['files']['name'][$key]);
          }
          }
          ?>


          3) Next, put an empty folder in that same directory, named "uploads", and make sure that folder has write permissions.

          4) To test, navigate to the page in step #1, click 'Browse', then hold down the 'ctrl' key to select multiple files at once with the mouse, then click 'Open'. Then click the 'Upload selected files' button. Once you've done that, the files should be uploaded to your "uploads" folder.

          That's the jist of it. Of course, you should also sanitize the POST data that's passed to the upload.php page, for security reasons. And you'll probably want to add some sort of message on that page that lets the user know whether or not the upload was successful.
          Signature

          {{ DiscussionBoard.errors[8275889].message }}
  • Profile picture of the author sktthemes
    Hello

    Multiple files can be selected and then uploaded using the
    <input type='file' name='file[]' multiple>

    The sample php script that does the uploading:
    <html>
    <?php
    session_start();
    $target=$_POST['directory'];
    if($target[strlen($target)-1]!='/')
    $target=$target.'/';
    $count=0;
    foreach ($_FILES['file']['name'] as $filename)
    {
    $temp=$target; $tmp=$_FILES['file']['tmp_name'][$count]; $count=$count + 1; $temp=$temp.basename($filename); move_uploaded_file($tmp,$temp); $temp=''; $tmp=''; } header("location: upload.php");
    ?>
    </html>
    {{ DiscussionBoard.errors[8318145].message }}
  • Profile picture of the author codingparty
    Originally Posted by webprogramzone View Post

    I have made code for uploading single file but i want to know how can i upload more than one file using php?

    Is coding same or should I modify something for it?
    Hey,

    if you are looking for an all-in-one solution you should check out the following scritpt:

    PHP Multifile Uploader for PHP 5.4, 5.5 | My Daily Hacks

    I have been using it for quite a while and like the way it provides a scaleable from. The AJAX progress bar is also quite handsome.

    In the post there also should be some hints how to configure PHP correctly. Hope, that helps
    {{ DiscussionBoard.errors[9691287].message }}
    • Profile picture of the author SteveJohnson
      Originally Posted by codingparty View Post

      ..Hope, that helps
      Since the original post/thread is well over a year old, no, it probably won't help.
      Signature

      The 2nd Amendment, 1789 - The Original Homeland Security.

      Gun control means never having to say, "I missed you."

      {{ DiscussionBoard.errors[9694279].message }}
    • Profile picture of the author bigshot90210
      I checked out your website and its 2015, and its still going. This is some very neat code. thanks! I am so excited, I will check out your other solutions for lot of stuff u have come across! Wow!
      Signature

      Get free information about my micro video
      http://pdsp.us/new_wso

      {{ DiscussionBoard.errors[10306138].message }}
  • {{ DiscussionBoard.errors[10308661].message }}
  • Profile picture of the author engineerz1
    Every programmer here.Please SUPPORT me and join my FB Page. Thanks in Advance

    https://www.facebook.com/The-Master-...?ref=bookmarks
    Signature

    Founder of The Master World

    {{ DiscussionBoard.errors[10313710].message }}
  • {{ DiscussionBoard.errors[10313711].message }}

Trending Topics