Page 1 of 1

Yet ANOTHER image upload question

Posted: Wed Aug 10, 2005 9:05 pm
by s.dot
As most of you should know by most of my posts, I deal extensively with images.. and uploading them.

My question is, say I have 10 file boxes for selecting images for upload.

When the user submits the form, I want do do a foreach();

if I do foreach($_FILES['filetoupload'] AS $pic) would $pic then contain all of the array values that $_FILES had? for example, ['tmp_name'], ['size'], etc?

Posted: Wed Aug 10, 2005 9:10 pm
by feyd
nope. You'll get each element of the normal $_FILES area, just as a larger array of crap

Posted: Wed Aug 10, 2005 9:17 pm
by s.dot
so erm, what's a good way to loop file uploads?

Edit: Nevermind, http://us2.php.net/features.file-upload has a great example.

Posted: Wed Aug 10, 2005 10:52 pm
by s.dot
PHP CODE:

Code: Select all

if($_POST['action'] == "uploadsubpictures")
{
	foreach($_FILES['filetoupload']['error'] as $key => $error)
	{
		if ($error == UPLOAD_ERR_OK)
		{
			$upload_dir = "uploads/$u/";
			$tmp_name = $_FILES['filetoupload']['tmp_name'][$key];
			$size = $_FILES['filetoupload']['size'][$key];
			$size_bytes = 1048576;
			if($size > $size_bytes)
			{
				echo "<p class=\"main\">File Too Large. Please try again.</p>"; exit();
			}
			$filename2 = mysql_real_escape_string(strip_tags($_FILES['filetoupload']['name'][$key]));
			$extension = strtolower(strrchr($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();
			}
			$time = time();
			$filename = $time.$extension;
			$filesize = getimagesize($tmp_name);
			include 'createthumb.inc.php';
			if(($filesize['0'] > 450) || ($filesize['1'] > 450))
			{
				move_uploaded_file($tmp_name,$upload_dir.$filename);
				chmod("uploads/$u/$filename", 0777);
				createthumb("uploads/$u/$filename","uploads/$u/$filename",450,450);
			} ELSE
			{
				move_uploaded_file($tmp_name,$upload_dir.$filename);
			}
			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());
   	}
	}
}
HTML FORM:

Code: Select all

<form action="editprofile.php?u=<? echo $u; ?>" method="post" enctype="multipart/form-data"> 
		Sub Pics<BR>
		<input type="hidden" name="action" value="uploadsubpictures">
		<input type="file" name="filetoupload[]" class="form"><BR>
		<input type="file" name="filetoupload[]" class="form"><BR>
		<input type="file" name="filetoupload[]" class="form"><BR>
		<input type="file" name="filetoupload[]" class="form"><BR>
		<input type="file" name="filetoupload[]" class="form"><BR>
		<input type="file" name="filetoupload[]" class="form"><BR>
		<input type="file" name="filetoupload[]" class="form"><BR>
		<input type="file" name="filetoupload[]" class="form"><BR>
		<input type="file" name="filetoupload[]" class="form"><BR>
		<input type="Submit" value="Upload">
		</form>
This code uploads the first picture fine, and then does nothing with the rest of the pictures. What am I missing that is causing this not to loop?

Posted: Thu Aug 11, 2005 12:28 pm
by s.dot
anyone?

Posted: Thu Aug 11, 2005 1:55 pm
by feyd
debug it :P

Posted: Thu Aug 11, 2005 5:12 pm
by s.dot
Very helpful :|







:P

Posted: Fri Aug 12, 2005 12:51 am
by John Cartwright
Actually that is a very valid suggestion.
Toss out a bunch of print_r's and echo's to understand what is happening with the data and compare it to what you were expecting.

Posted: Fri Aug 12, 2005 6:01 am
by bokehman
For me, using a three field form, for uploading one test file, this: foreach($_FILES['file'] AS $pic){ var_dump($pic);} returned this:

Code: Select all

array(3) {
  [0]=>
  string(8) "news.php"
  [1]=>
  string(0) ""
  [2]=>
  string(0) ""
}
array(3) {
  [0]=>
  string(24) "application/octet-stream"
  [1]=>
  string(0) ""
  [2]=>
  string(0) ""
}
array(3) {
  [0]=>
  string(26) "C:\WINDOWS\TEMP\php801.tmp"
  [1]=>
  string(0) ""
  [2]=>
  string(0) ""
}
array(3) {
  [0]=>
  int(0)
  [1]=>
  int(4)
  [2]=>
  int(4)
}
array(3) {
  [0]=>
  int(317)
  [1]=>
  int(0)
  [2]=>
  int(0)
}
Instead of all that nonsense maybe you should just access the files array directly. It would return this:

Code: Select all

array(5) {
  ["name"]=>
  array(3) {
    [0]=>
    string(9) "forum.php"
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
  }
  ["type"]=>
  array(3) {
    [0]=>
    string(24) "application/octet-stream"
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
  }
  ["tmp_name"]=>
  array(3) {
    [0]=>
    string(26) "C:\WINDOWS\TEMP\php80A.tmp"
    [1]=>
    string(0) ""
    [2]=>
    string(0) ""
  }
  ["error"]=>
  array(3) {
    [0]=>
    int(0)
    [1]=>
    int(4)
    [2]=>
    int(4)
  }
  ["size"]=>
  array(3) {
    [0]=>
    int(319)
    [1]=>
    int(0)
    [2]=>
    int(0)
  }
}

Posted: Fri Aug 12, 2005 7:16 am
by bokehman
Personally I think what you are looking for is something like the following which converts the FILES array into a more manageable form. This:

Code: Select all

for($i = 0, $count = count($_FILES['filetoupload']['name']); $i < $count; $i++){
	foreach($_FILES['filetoupload'] as $key => $value){
		if(!empty($_FILES['file']['name'][$i])){
			$pic[$i][$key] = str_replace('\\', '/', $value[$i]);
		}
	}
}
produces an array like this:

Code: Select all

Array
(
    [0] => Array
        (
            [name] => template.php
            [type] => application/octet-stream
            [tmp_name] => C:/WINDOWS/TEMP/php8A7.tmp
            [error] => 0
            [size] => 5956
        )

    [1] => Array
        (
            [name] => forum.php
            [type] => application/octet-stream
            [tmp_name] => C:/WINDOWS/TEMP/php8A8.tmp
            [error] => 0
            [size] => 319
        )

    [2] => Array
        (
            [name] => index.php
            [type] => application/octet-stream
            [tmp_name] => C:/WINDOWS/TEMP/php8A9.tmp
            [error] => 0
            [size] => 318
        )

)
I hope that is of some help to you!