[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;
?>
14 Comments:
thanks for the script.
is there a way to limit the file size?
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 !
awsome
awsome
What an awesome script...
Worked first time around :) Thanks.
Modified it a lil to add and retrieve from a database.
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
@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
Wrong. Theres a typo. $_FILES[]'uploded_
got an extra closing brace.
how to save this to mysql database? and how to catch the image name of the picture.?
ASAP
how to save this to mysql database? and how to catch the image name of the picture.?
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!
you need to create a new directory with the name "uploads"
Great script man.........
how do your retrieve images from the database?
Post a Comment