Page 1 of 1

looping file uploads [SOLVED]

Posted: Sun Sep 04, 2005 12:57 pm
by s.dot
check out this code:

Code: Select all

if($_POST['action'] == "uploadsubpictures")
{
  print_r($_FILES['filetoupload']['tmp_name']);
}
This outputs:

Code: Select all

Array ( [0] => /tmp/phpaYw8OY [1] => /tmp/phpyF3VSj [2] => /tmp/php8EfvoK [3] => /tmp/phpYlSZAp [4] => [5] => [6] => )
That looks good to me, as I only put 4 files into the form. So far so good.
Then I add this piece of code:

Code: Select all

if($_POST['action'] == "uploadsubpictures")
{
  foreach($_FILES['filetoupload']['tmp_name'] AS $key => $value)
  {
    echo 'Key: '.$key.' => Value: '.$value.'<BR />';
  }
}
This outputs:

Code: Select all

Key: 0 => Value: /tmp/phpPqaAUd
Key: 1 => Value: /tmp/phpKKL9i9
So where are the other 2 values going?

Posted: Sun Sep 04, 2005 1:12 pm
by feyd
did you upload 4 files? did the files get uploaded correctly? (correct size and junk)

Posted: Sun Sep 04, 2005 1:21 pm
by s.dot
The complete code, with debugging information:

Code: Select all

if($_POST['action'] == "uploadsubpictures")
{
  // print entire $_FILES array
  print_r($_FILES);
  echo '<BR /><BR />';

  // print the tmp_name array
  print_r($_FILES['filetoupload']['tmp_name']);
  echo '<BR /><BR />';

	foreach($_FILES['filetoupload']['tmp_name'] AS $key => $value)
	{
		// print each key => value
		echo 'Key: '.$key.' => Value: '.$value.'<BR />';
  }
}
The output:

Code: Select all

Array ( [filetoupload] => Array ( [name] => Array ( [0] => Blue hills.jpg [1] => Sunset.jpg [2] => Water lilies.jpg [3] => Winter.jpg [4] => [5] => [6] => [7] => ) [type] => Array ( [0] => image/pjpeg [1] => image/pjpeg [2] => image/pjpeg [3] => image/pjpeg [4] => [5] => [6] => [7] => ) [tmp_name] => Array ( [0] => /tmp/phpWknhVL [1] => /tmp/phpdeXq0v [2] => /tmp/phpAAIRsl [3] => /tmp/php9ZxIKo [4] => [5] => [6] => [7] => ) [error] => Array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 [4] => 4 [5] => 4 [6] => 4 [7] => 4 ) [size] => Array ( [0] => 28521 [1] => 71189 [2] => 83794 [3] => 105542 [4] => 0 [5] => 0 [6] => 0 [7] => 0 ) ) ) 

Array ( [0] => /tmp/phpWknhVL [1] => /tmp/phpdeXq0v [2] => /tmp/phpAAIRsl [3] => /tmp/php9ZxIKo [4] => [5] => [6] => [7] => ) 

Key: 0 => Value: /tmp/phpWknhVL
Key: 1 => Value: /tmp/phpdeXq0v
As you can see, all four files pass nicely until it reaches the foreach(), and then it only grabs key 0 and 1 from the tmp_name array

Posted: Sun Sep 04, 2005 3:04 pm
by bokehman

Code: Select all

Array ( 
	[filetoupload] => Array ( 
		[name] => Array ( 
				[0] => Blue hills.jpg 
				[1] => Sunset.jpg 
				[2] => Water lilies.jpg 
				[3] => Winter.jpg 
				[4] => 
				[5] => 
				[6] => 
				[7] => 
			) 
	[type] => Array ( 
				[0] => image/pjpeg 
				[1] => image/pjpeg 
				[2] => image/pjpeg 
				[3] => image/pjpeg 
				[4] => 
				[5] => 
				[6] => 
				[7] => 
			) 
	[tmp_name] => Array ( 
				[0] => /tmp/phpWknhVL 
				[1] => /tmp/phpdeXq0v 
				[2] => /tmp/phpAAIRsl 
				[3] => /tmp/php9ZxIKo 
				[4] => 
				[5] => 
				[6] => 
				[7] => 
			) 
	[error] => Array ( 
				[0] => 0 
				[1] => 0 
				[2] => 0 
				[3] => 0 
				[4] => 4 
				[5] => 4 
				[6] => 4 
				[7] => 4 
			) 
	[size] => Array ( 
				[0] => 28521 
				[1] => 71189 
				[2] => 83794 
				[3] => 105542 
				[4] => 0 
				[5] => 0 
				[6] => 0 
				[7] => 0 
			) 
	)
)

Posted: Sun Sep 04, 2005 10:45 pm
by s.dot
yah that's what I posted :-D

Posted: Sun Sep 04, 2005 11:19 pm
by s.dot
okay so the foreach() only grabs the first 2 values, but if I do this

Code: Select all

for($i=0;$i<9;$i++)
{
    echo $_FILES['filetoupload']['tmp_name'][$i].'<BR />';
}
It will show me all of the values.

Isn't this a bit.............. odd?

Posted: Sun Sep 04, 2005 11:27 pm
by feyd
it seems you have an issue with the build of php you are using. Have you tried this with a different build?

Posted: Sun Sep 04, 2005 11:29 pm
by josh
scrotaye wrote:Isn't this a bit.............. odd?
YES



try this:

Code: Select all

while (list($key, $value) = each($arr)) {
    // stuff
}

Posted: Mon Sep 05, 2005 12:08 am
by s.dot
Okay, wow. I didn't touch the code even one character from earlier to now, and now the foreach is showing all of the values.

I don't understand how this could be. Perhaps someone on my host server was playing around with PHP? I don't know. I think I'll just stick with the for($i=0.... loop. For stability. :?

Posted: Mon Sep 05, 2005 1:14 am
by s.dot
Okay, well on to the rest of the problem. I've stripped the loop down to the bare essentials it needs to loop through and upload the images. I can't seem to figure out why it won't work!

Code:

Code: Select all

if($_POST['action'] == "uploadsubpictures")
{
	$upload_dir = "uploads/$u/";
	for($i=0;$i<9;$i++)
	{
		if($_FILES['filetoupload']['name'][$i])
		{
			$tmp_name = $_FILES['filetoupload']['tmp_name'][$i];
			$filename2 = mysql_real_escape_string(strip_tags($_FILES['filetoupload']['name'][$i]));
			$extension = strtolower(strchr($filename2,"."));
			$extensions = array('.jpg','.jpeg','.png');
			if(!in_array($extension,$extensions))
			{
				echo "<p class=\"main\"><font color=\"red\">File type not allowed.  Only JPG or PNG files are allowed.</font></p>";
				require 'footer.php';
				die();
			}
			$str = rand(0,9999999);
			$filename = $str.$extension;
			include 'createthumb.inc.php';
			createthumb("uploads/$u/$filename","thumbs/$u/$filename",100,100);
			$size2 = filesize("uploads/$u/$filename");
			mysql_query("INSERT INTO subpics (username, img, size) VALUES('$u','$filename','$size2')") or die(mysql_error());	
		}
	}
}
This will take the first picture and do everything correctly with it. But after that it doesn't seem to do anything. Nothing is output when the script is done running (not even futher html after this code on the page) so I'm assuming the script "dies" somewhere. Does anyone see why?

Posted: Mon Sep 05, 2005 1:18 am
by feyd
you could name them all uniquely, making the looping a bit easier to handle ;)

Posted: Mon Sep 05, 2005 1:21 am
by s.dot

Code: Select all

$str = rand(0,9999999); 
$filename = $str.$extension;
isn't that giving them a unique name :P

Posted: Mon Sep 05, 2005 1:23 am
by feyd
I was referring to the names of the upload fields...

Posted: Mon Sep 05, 2005 2:47 am
by s.dot
having include 'createthumb.inc.php'; inside of the loop was causing a problem.

I put it outside of the loop (dunno why I had it in there in the first place) and all is well.

Posted: Mon Sep 05, 2005 9:52 am
by John Cartwright
fyi in the future when you know you only need to access a file once, include_once :wink: