Multi img upload almost works

I had this thing working for a single image. I then added:

$counter = 3;
for ($i = 0; $i < $counter; $i++) { to surround the code that already works. }

I then changed the form at the bottom which might be wrong.

Can someone spot the obvious that I can’t see. Thanks.


<?php	
define ('MAX_FILE_SIZE', 3000000); 
define('UPLOAD_DIR', 'upload_test/');

$counter = 3;
if( isset($_POST['upload']) ){

	for ($i = 0; $i < $counter; $i++) {

		if (!empty($_FILES['img']['name'])){
			
			$imgnm = $_FILES['img']['name'];
			move_uploaded_file($_FILES['img']['tmp_name'], UPLOAD_DIR.$imgnm);
			$udimgnm = UPLOAD_DIR.$imgnm;
		
			$typeok = TRUE;
	
			switch($_FILES['img']['type']){
		
				case "image/gif":   $src = imagecreatefromgif($udimgnm); break;

				case "image/jpeg":  // Both regular and progressive jpegs
				case "image/pjpeg":	$src = imagecreatefromjpeg($udimgnm); break;

				case "image/png":   $src = imagecreatefrompng($udimgnm); break;

				default:			$typeok = FALSE; break;
			}
		
			if($typeok){
					
			list($w, $h) = getimagesize($udimgnm);
			$max = 100;
			$tw  = $w;
			$th  = $h;
		
			if ($w > $h && $max < $w)
			{
				$th = $max / $w * $h;
				$tw = $max;
			}
			elseif ($h > $w && $max < $h)
			{
				$tw = $max / $h * $w;
				$th = $max;
			}
			elseif ($max < $w)
			{
				$tw = $th = $max;
			}
		
			$tmp = imagecreatetruecolor($tw, $th);
			imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
			imageconvolution($tmp, array( // Sharpen image
									array(-1, -1, -1),
									array(-1, 16, -1),
									array(-1, -1, -1)
									), 8, 0);
			imagejpeg($tmp, $udimgnm);
			imagedestroy($tmp);
			imagedestroy($src);			
				
			}else
				echo "<p>error with image upload.</p>";
			
		}
	}
}
?>

<form action="post_s3.php" method="post" enctype="multipart/form-data">

	<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
	
	<?php // Create the inputs.
	for ($i = 0; $i < $counter; $i++) {
		echo '<p><input type="file" name="img" /></p>';
	}
	?>

	<!--<input type="hidden" name="submitted" value="TRUE" />-->
    <input type="submit" value="upload image" name="upload"/>
	  
</form>

Why must there be brackets “”? I’ve never seen this before.

name=“file

The new form should be used with the modified version of the script that I gave you in post #2

quick easy way to upload multiple items… and you can add your own php script in to process the files how ever you want.

I notice there is no submit button.

<form name=“form” enctype=“multipart/form-data” method=“post” action=“test.php”>
<table border=“0” cellspacing=“0” cellpadding=“0”>
<tr>
<td style=“vertical-align: top; width: 50%;”>
<div class=“header”>Files</div><div style=“font-size: 90%;”>Files entered here will be transferred to the server.</div><br />
</td>
</tr>
<tr>
<td style=“vertical-align: top; width: 50%;”>
<?php for ($i = 0; $i < $counter; $i++){ ?>
<input type=“file” name=“file” /><br />
<?php } ?>
<span id=“file_1”></span><br />
</td>
</tr>
</table>

So I added this.

<input type=“submit” value=“upload image” name=“upload”/>

</form>

I can’t seem to make this work. Does name=“file” need to be placed where ‘img’ is above?

if (!empty($_FILES[‘img’][‘name’])){

So it looks like this for all the ‘img’ s.

if (!empty($_FILES[‘file’][‘name’])){

Thanks.

Try this:

upload.js


function add_file(id, i)
{
	if (document.getElementById(id + '_' + i).innerHTML.search('uploadinputbutton') == -1)
	{
		document.getElementById(id + '_' + i).innerHTML = '<input type="file" class="uploadinputbutton" maxsize="" name="' + id + '[]" onchange="return add_file(\\'' + id + '\\', ' + (i+1) + ');" /><br /><span id="' + id + '_' + (i+1) + '"><input type="button" value="Add other" onclick="add_file(\\'' + id + '\\', ' + (i+1) + ');" /><\\/span>\
';
	}
}


<?php

define('MAX_FILE_SIZE', 3000000);
define('UPLOAD_DIR', 'upload_test/');

$counter = 3;

if (isset($_POST['upload']))
{
  for ($i = 0; $i < $counter; $i++)
  {
	  if (!empty($_FILES['file']['name'][$i]))
	  {
		  $filenm = $_FILES['file']['name'][$i];

		  move_uploaded_file($_FILES['file']['tmp_name'][$i], UPLOAD_DIR . $filenm);

		  $udfilenm = UPLOAD_DIR . $filenm;
		  
		  $typeok = true;
		  
		  switch ($_FILES['file']['type'][$i])
		  {
			  case "image/gif":
				  $src = imagecreatefromgif($udfilenm);
				  break;
				  
			  case "image/jpeg":
				  // Both regular and progressive jpegs 
			  case "image/pjpeg":
				  $src = imagecreatefromjpeg($udfilenm);
				  break;
				  
			  case "image/png":
				  $src = imagecreatefrompng($udfilenm);
				  break;
				  
			  default:
				  $typeok = false;
				  break;
		  }
		  
		  if ($typeok)
		  {
			  list($w, $h) = getimagesize($udfilenm);
			  $max = 100;
			  $tw = $w;
			  $th = $h;
			  
			  if ($w > $h && $max < $w)
			  {
				  $th = $max / $w * $h;
				  $tw = $max;
			  }
			  elseif ($h > $w && $max < $h)
			  {
				  $tw = $max / $h * $w;
				  $th = $max;
			  }
			  elseif ($max < $w)
			  {
				  $tw = $th = $max;
			  }
			  
			  $tmp = imagecreatetruecolor($tw, $th);
			  imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
			  imageconvolution($tmp, array(// Sharpen image 
			  array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
			  imagejpeg($tmp, $udfilenm);
			  imagedestroy($tmp);
			  imagedestroy($src);
		  }
		  else
			  echo "<p>error with image upload.</p>";
	  }
  }
}
?>
<head>
<title>Untitled Document</title>
<script type="text/javascript" src="upload.js"></script>
</head>
<body>
<form name="form" enctype="multipart/form-data" method="post" action="test.php">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
	<td style="vertical-align: top; width: 50%;">
		<div class="header">Files</div><div style="font-size: 90%;">Files entered here will be transferred to the server.</div><br />
	</td>
</tr>
<tr>
	<td style="vertical-align: top; width: 50%;">
		<input type="file" name="file[]" onchange="add_file('file', 1);" /><br />
		<span id="file_1"><input type="button" value="Add another" onclick="add_file('file', 1);" /></span><br />
	</td>
</tr>
</table>
<input type="submit" name="upload" value="submit" />
</form>
</body>

Even better:


<form name="form" enctype="multipart/form-data" method="post" action="test.php"> 
<table border="0" cellspacing="0" cellpadding="0"> 
<tr> 
<td style="vertical-align: top; width: 50%;"> 
<div class="header">Files</div><div style="font-size: 90%;">Files entered here will be transferred to the server.</div><br /> 
  </td> 
</tr> 
<tr> 
  <td style="vertical-align: top; width: 50%;">
<?php for ($i = 0; $i < 3; $i++){ 
	echo "File " . $i ?> <input type="file" name="file[]" /><br />
<?php } ?>
  <span id="file_1"></span><br /> 
  </td> 
</tr> 
</table>
<input type="submit" name="upload" value="submit" />
</form>

Adding the brackets automatically creates an array server-side containing the entries.

I see.

I guess name=“file” holds $i in all the ($_FILES[‘file’][‘name’][$i]), but the brackets are empty, “file”.

Adding the brackets automatically creates an array server-side containing the entries. :wink:

I don’t know what that is.

Thanks.

That looks a lot simpler. I will need some time to study and test it.

use uploadify :slight_smile:

Maybe this:


<form name="form" enctype="multipart/form-data" method="post" action="test.php"> 
<table border="0" cellspacing="0" cellpadding="0"> 
<tr> 
<td style="vertical-align: top; width: 50&#37;;"> 
 <div class="header">Files</div><div style="font-size: 90%;">Files entered here will be transferred to the server.</div><br /> 
  </td> 
 </tr> 
 <tr> 
  <td style="vertical-align: top; width: 50%;">
<?php for ($i = 0; $i < $counter; $i++){ ?>
  <input type="file" name="file[]" /><br />
<?php } ?>
  <span id="file_1"></span><br /> 
  </td> 
</tr> 
</table>

Thank you sir.

Your solution worked.

Thanks for your response.

Is JavaScript required to make this work? I don’t like using JavaScript because I am concerned it may be disabled in the clients browser plus I don’t understand JavaScript enough yet to deal with it. Thanks for your suggestion and I will study it.

Can anyone else see how this might work?