multiple image upload script with error
04-13-10, 11:30 AM
Newbie Coder
Join Date: Nov 2009
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
multiple image upload script with error
Hi, im trying to make it possible for user to upload 5 images with this script but i get the error msg : Error Uploading file! Code Array.
any ideas to what i can do to solve this?
PHP Code:
<?php
$filename = array( " ' $HTTP_POST_FILES [ ufile ] [name][0]',' $HTTP_POST_FILES [ ufile ] ['name'][1]',' $HTTP_POST_FILES [ ufile ] [name][2]'
,' $HTTP_POST_FILES [ ufile ] [name][3]',' $HTTP_POST_FILES [ ufile ] [name][4]' " );
$filetype = array( " ' $HTTP_POST_FILES [ ufile ] [size][0]',' $HTTP_POST_FILES [ ufile ] [size][1]',' $HTTP_POST_FILES [ ufile ] [size][2]','
$HTTP_POST_FILES [ ufile ] [size][3]',' $HTTP_POST_FILES [ ufile ] [size][4]' " );
$filesize = array ( " ' $HTTP_POST_FILES [ ufile ] [type][0]', ' $HTTP_POST_FILES [ ufile ] [type][1]',' $HTTP_POST_FILES [ ufile ] [type][2]','
$HTTP_POST_FILES [ ufile ] [type][3]',' $HTTP_POST_FILES [ ufile ] [type][4]' " );
$filetemp = array ( " ' $HTTP_POST_FILES [ ufile ] [tmp_name][0]',' $HTTP_POST_FILES [ ufile ] [tmp_name][1]',' $HTTP_POST_FILES [ ufile ] [tmp_name][2]','
$HTTP_POST_FILES [ ufile ] [tmp_name][3]',' $HTTP_POST_FILES [ ufile ] [tmp_name][4]' " );
$error = $_FILES [ "ufile" ][ "error" ];
if ( $error > 0 )
die ( "Error Uploading file! Code $error ." );
else
{
if ( $filetype != ".jpeg" || ".png" || ".gif" || ".bmp" || $filesize < 2000000 ) // condition for the file.
{
die ( "Format is not allowed or file size is too big!" );
}
else
{
move_uploaded_file ( $filetemp , " uploaded/" . $filename );
echo "Upload complete!" ;
}
}
?>
04-13-10, 05:02 PM
Aspiring Coder
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
1. You are not building your array's correctly.
PHP: Arrays - Manual
2. You are not using arrays correctly in your if/else statements.
3. move_uploaded_file() does not accept arrays as an argument.
PHP: move_uploaded_file - Manual
04-13-10, 09:05 PM
Community Liaison
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Here is one possible method for uploading multiple files.
__________________
Jerry Broughton
04-14-10, 03:07 PM
Newbie Coder
Join Date: Nov 2009
Posts: 14
Thanks: 3
Thanked 0 Times in 0 Posts
Hi, dont get any error msg using this script but i also dont get any results( upload complete) when i try to echo:
<?php $_FILES["ufile0"]["name"]; echo "<img src= 'upload/".$_FILES["ufile$i"]["name"]." '>";?>
i only get broken images. any ideas why. ive been on it all day.
04-14-10, 05:56 PM
Community Liaison
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
I added an array, $imageFiles, to store the path/filename, which I use in a for loop just before the form is displayed again.
Try it like this:
PHP Code:
<?php if(!empty( $_POST [ "upload" ])){ $FileErrorMessages = array( "1" => "The uploaded file exceeds the upload_max_filesize directive in php.ini." , "2" => "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form." , "3" => "The uploaded file was only partially uploaded." , "4" => "No file was uploaded." ); $filetype = array( "image/pjpeg" , "image/jpeg" , "image/png" , "image/gif" , "image/bmp" ); for( $i = 0 ; $i < count ( $_FILES ); $i ++){ if( $_FILES [ "ufile $i " ][ "error" ] != 0 || ! in_array ( $_FILES [ "ufile $i " ][ "type" ], $filetype ) || $_FILES [ "ufile $i " ][ "size" ] > 2000000 ){ $fileError [] = "Name: " . $_FILES [ "ufile $i " ][ "name" ]. "<br />Size: " . $_FILES [ "ufile $i " ][ "size" ]. "<br />Type: " . $_FILES [ "ufile $i " ][ "type" ]. "<br />Temp name: " . $_FILES [ "ufile $i " ][ "tmp_name" ]. "<br />Error: " . $FileErrorMessages [ $_FILES [ "ufile $i " ][ "error" ]]; } else{ move_uploaded_file ( $_FILES [ "ufile $i " ][ "tmp_name" ], "uploaded/" . $_FILES [ "ufile $i " ][ "name" ]); $imageFiles [] = "uploaded/" . $_FILES [ "ufile $i " ][ "name" ]; } } echo "<p><h2>Upload complete!</h2></p>" ; if(isset( $fileError )){ echo "<div style='color:#f00;border:1px solid #000;padding:10px;width:25%;'>Problem uploading these files.<br />------------------------------<p>" ; for( $i = 0 ; $i < count ( $fileError ); $i ++){ echo $fileError [ $i ]. "<p>" ; } echo "</div>" ; } } ?> <html> <head> <title>Multiple file upload</title> </head> <body><?php if(isset( $imageFiles )) { for( $i = 0 ; $i < count ( $imageFiles ); $i ++) { $fileParts = explode ( "/" , $imageFiles [ $i ]); echo "<p><div><img src='" . $imageFiles [ $i ]. "' /><br /><span style='font-size:20px;font-weight:bold;margin-top:10px;'>" . $fileParts [ 1 ]. "</span></div></p>" ; } } ?> <form action="#" method="POST" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="2000000" /> <input type="file" name="ufile0" /><br /> <input type="file" name="ufile1" /><br /> <input type="file" name="ufile2" /><br /> <input type="file" name="ufile3" /><br /> <input type="file" name="ufile4" /><br /> <input type="submit" name="upload" value="Upload" /> </form> </body> </html>
__________________
Jerry Broughton
Last edited by job0107; 04-14-10 at 06:06 PM .
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off