Wednesday, August 02, 2006

[PHP] - [Upload Multiple Images]

[Initial Setup]
1. Create a folder "uploads" on the server
2. Create file.php and upload.php and use the code below


[file.php]
<form enctype="multipart/form-data" action="upload.php" method="post">
Image1: <input name="userfile[]" type="file" /><br />
Image2: <input name="userfile[]" type="file" /><br />
Image3: <input name="userfile[]" type="file" /><br />
Image4: <input name="userfile[]" type="file" /><br />
<input type="submit" value="Upload" />
</form>


[upload.php]
<?php
  $success = 0;
  $fail = 0;
  $uploaddir = 'uploads/';
  for ($i=0;$i<4;$i++)
  {
   if($_FILES['userfile']['name'][$i])
   {
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name'][$i]);
    $ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
    if (preg_match("/(jpg|gif|png|bmp)/",$ext))
    {
     if (move_uploaded_file($_FILES['userfile']['tmp_name'][$i], $uploadfile))
     {
      $success++;
     }
     else
     {
     echo "Error Uploading the file. Retry after sometime.\n";
     $fail++;
     }
    }
    else
    {
     $fail++;
    }
   }
  }
  echo "<br> Number of files Uploaded:".$success;
  echo "<br> Number of files Failed:".$fail;
?>

Bookmark and Share

14 Comments:

Blogger Unknown said...

thanks for the script.

is there a way to limit the file size?

June 20, 2008 3:13 AM  
Anonymous Anonymous said...

Thank you for this script too !

I have a script for upload in my script, but this still better when uploading many files.
Please see my script upload
and tell me what you think.

See you later !

August 14, 2008 11:46 PM  
Blogger Unknown said...

awsome

November 09, 2008 12:40 AM  
Blogger Unknown said...

awsome

November 09, 2008 12:41 AM  
Anonymous Anonymous said...

What an awesome script...
Worked first time around :) Thanks.
Modified it a lil to add and retrieve from a database.

November 13, 2008 12:35 PM  
Blogger Ilango said...

Hi
I tried out your script today.
I am getting the following error:
Notice: undefined index: uploaded_file in ... on line 18
And line 18 is:
if($_FILES[]'uploaded_file']['name'][$i])..

Am I missing something. Thanks in advance

October 03, 2009 1:14 AM  
Blogger Deepak Radhakrishnan said...

@Samantha & Ilango: You have to initialize $i to avoid this error.

You can also change settings in your php.ini file settings

error_reporting = E_ALL & ~E_NOTICE

October 20, 2009 3:47 AM  
Anonymous Anonymous said...

Wrong. Theres a typo. $_FILES[]'uploded_
got an extra closing brace.

November 21, 2009 2:41 AM  
Blogger Unknown said...

how to save this to mysql database? and how to catch the image name of the picture.?

ASAP

November 21, 2009 9:50 PM  
Blogger Johnef said...

how to save this to mysql database? and how to catch the image name of the picture.?

December 17, 2010 7:50 PM  
Anonymous Anonymous said...

I'm getting the following error:
Warning: move_uploaded_file(test/uploads/a.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /var/www/test/upload.php on line 13

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpkK8f6J' to 'test/uploads/a.jpg' in /var/www/test/upload.php on line 13
I don/t know what to do.
Thanks!

August 23, 2011 10:25 PM  
Blogger Deepak Radhakrishnan said...

you need to create a new directory with the name "uploads"

August 23, 2011 10:38 PM  
Anonymous Anonymous said...

Great script man.........

February 04, 2013 12:34 PM  
Anonymous Anonymous said...

how do your retrieve images from the database?

February 13, 2013 7:13 PM  

Post a Comment