Current location: Hot Scripts Forums » Programming Languages » PHP » multiple image upload script with error


multiple image upload script with error

Reply
  #1 (permalink)  
Old 04-13-10, 11:30 AM
balamberas balamberas is offline
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!";
}
}

?>
Reply With Quote
  #2 (permalink)  
Old 04-13-10, 05:02 PM
Jcbones Jcbones is offline
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
Reply With Quote
  #3 (permalink)  
Old 04-13-10, 09:05 PM
job0107's Avatar
job0107 job0107 is offline
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.
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"] != || !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"]);
       }
   }
   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>
<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
Reply With Quote
  #4 (permalink)  
Old 04-14-10, 03:07 PM
balamberas balamberas is offline
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.


PHP Code:



if(!empty($_POST["submit"])){
   
$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"] != || !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"]," upload/".$_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>";
   }

Reply With Quote
  #5 (permalink)  
Old 04-14-10, 05:56 PM
job0107's Avatar
job0107 job0107 is offline
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"] != || !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.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
image upload script + [img] tag url generation for forums zoog Script Requests 1 01-20-10 05:53 PM
Using script to upload files onto server hansadamsson70 Script Requests 1 11-05-09 03:28 PM
PHP Script Error - Can you please help? (Goal, Script and Error Posted Below EnergyAppeal PHP 1 07-19-09 04:21 PM
PHP Script Request DazzlyWorks Script Requests 0 01-16-05 01:23 PM
Image upload and if / else mdhall PHP 4 11-14-04 03:12 PM


All times are GMT -5. The time now is 08:14 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.