Home » jQuery, PHP

Multiple file upload with jQuery and php

29 June 2009 13,708 views Popularity: 25% Share/Bookmark

Here, I am going to show you how easy it is to upload multiple files with the help of jQuery and PHP.

I suppose that you are familiar with php and apache web server.

Let our folder name be ‘test’. Let us create a file called ‘index.php’ inside test folder. Inside the ‘test’ folder, you have to make another folder named ‘uploads’. The files will be uploaded into this folder.

We need few more things. We need the jQuery js files. Download the following two files and keep inside the folder ‘test’.

http://jquery.com/src/jquery-latest.js

http://jquery-multifile-plugin.googlecode.com/svn/trunk/jquery.MultiFile.js

Below is the complete code for multiple file upload:

<?php
if(isset($_POST['upload']))
{
    $uploaddir = 'uploads/';
    foreach ($_FILES["pic"]["error"] as $key => $error)
    {
        if ($error == UPLOAD_ERR_OK)
        {
            $tmp_name = $_FILES["pic"]["tmp_name"][$key];
            $name = $_FILES["pic"]["name"][$key];
            $uploadfile = $uploaddir . basename($name);

            if (move_uploaded_file($tmp_name, $uploadfile))
            {
                echo "Success: File ".$name." uploaded.<br/>";
            }
            else
            {
                echo "Error: File ".$name." cannot be uploaded.<br/>";
            }
        }
    }
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Multiple File Upload with jQuery and PHP</title>

<script src="jquery.js" type="text/javascript" language="javascript"></script>
<script src="jquery.MultiFile.js" type="text/javascript" language="javascript"></script>

</head>

<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="pic[]" class="multi" />
<input type="submit" name="upload" value="Upload" />
</form>
</body>

</html>

There is one important thing to be noticed.

<input type="file" name="pic[]" class="multi" />

You have add “[ ]” to name of your element. And you also have to add class=”multi”.

For more examples, see:

http://www.fyneworks.com/jquery/multiple-file-upload/#tab-Examples

From Mukesh Chapagain's Blog, post Multiple file upload with jQuery and php

Related posts:

  1. File Upload in PHP :: Simplified
  2. Magento: How to upload file?
  3. Magento jQuery: How to use them together?
  4. File Upload in PEAR and Smarty
  5. jQuery: Print array and object
  6. jQuery: Set time interval between events with queue function
  7. jQuery: A simple Slideshow
  8. jQuery: How to replace string, div content and image src?
  9. jQuery: Animate and Transfer effect with Image
  10. jQuery: Preview Image with Tooltip Effect
  • Bil0313

    Thanks a lot..,but u said multi.but there is only one textbox,when i try to add more textbox 2 upload multiple files i am getting error,can u plz help??i need to add a second textbox to upload files simultaneously..

  • http://www.roosle.com File Upload

    successful application :) Thanks…

  • Pennix

    I have the same issue with Bil0313.
    Is there a way to customize the script in order to have the ability to select more than one pic at the time (ie. by pressing the shift key) ?
    Thanks in advance,
    Pennix

  • Pennix

    I have a possible solution to this by adding the following link in the html part:

    The issue that I’m facing now is that the list of the selected files contains only the first one.
    Is there a way to append the rest of the filenames to the list? I have tried looping through the list.append , addToList etc..with no luck.
    Any ideas really appreciated!

  • pennix

    Hello Bil,
    Didi you find any solution to the problem that you describe?
    Thanks in advance

  • http://fiveholiday55.blogspot.com Helen Neely

    Your title said multiple file upload, but it seems like a single file upload script to me. Wanna modify the title then?

  • http://www.facebook.com/people/Shivaraj-Halegowdars/100000355595425 Shivaraj Halegowdars

    Thanks a lot…
    I like this article lot

  • http://www.facebook.com/people/Shivaraj-Halegowdars/100000355595425 Shivaraj Halegowdars

    Hi mukesh can u tell very good books for PHP and JQuery?

  • Valluriravikumar

    The file jquery.MultiFile.js is not working in Overlay window(Pop window). Can anyone tell the solution for the same.

  • Nivahada

    how  to  insert those file in database