Yet ANOTHER image upload question

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

Yet ANOTHER image upload question

Post 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?
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 »

nope. You'll get each element of the normal $_FILES area, just as a larger array of crap
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
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 »

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?
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 »

anyone?
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 »

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

Post by s.dot »

Very helpful :|







: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
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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)
  }
}
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post 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!
Post Reply