Upload multiple files using php

by 11 replies
16
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?
#programming #files #multiple #php #upload
  • 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");
    }
    }
    ?>
    • [1] reply
    • 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>
      • [1] reply
  • 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>
  • 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
    • [ 1 ] Thanks
    • [2] replies
    • Since the original post/thread is well over a year old, no, it probably won't help.
      • [ 1 ] Thanks
    • 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!
  • [DELETED]
  • I use dropzone.js.
    Check it, it's a great (and free) script for this work!
  • Every programmer here.Please SUPPORT me and join my FB Page. Thanks in Advance

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

Next Topics on Trending Feed