looping file uploads [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

looping file uploads [SOLVED]

Post 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?
Last edited by s.dot on Mon Sep 05, 2005 2:47 am, edited 1 time in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

did you upload 4 files? did the files get uploaded correctly? (correct size and junk)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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 
			) 
	)
)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

yah that's what I posted :-D
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it seems you have an issue with the build of php you are using. Have you tried this with a different build?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

scrotaye wrote:Isn't this a bit.............. odd?
YES



try this:

Code: Select all

while (list($key, $value) = each($arr)) {
    // stuff
}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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. :?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you could name them all uniquely, making the looping a bit easier to handle ;)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

$str = rand(0,9999999); 
$filename = $str.$extension;
isn't that giving them a unique name :P
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I was referring to the names of the upload fields...
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

fyi in the future when you know you only need to access a file once, include_once :wink:
Post Reply