Jump to content

[SOLVED] handling multiple file uploads


wrathican

Recommended Posts

hey peeps thanks for looking.

 

im trying to make an image upload script that creates a thumbnail and a smaller than original size version of the image.

 

im using JS to add extra form fields to my form dynamically.

so once a user has selected a file in the first box, another box appears and so on until there are 5 boxes.

 

i can send the files to a script but at the moment it is just a testing one that echoes out the tmp name and actualt file name of the file(s) selected

if i select 5 files and submit the output looks like this: (which means i have 5 boxes each containing something)

/tmp/phpkexbXx - landscape_big15.jpg
/tmp/phpOzAYCn - portrait_big.jpg
/tmp/php4Tk5Te - landscape_big16.jpg
/tmp/phpsXraM7 - landscape_big17.jpg
/tmp/phpKvmxe2 - landscape_big19.jpg

 

but if i only select 2 files my output looks like this: (which means 2 fields have input and there is one blank one)

/tmp/phpmYw9d3 - landscape_big15.jpg
/tmp/phpqwqCpP - landscape_big9.jpg
-
-
- 

 

i guess when it comes to creating a thumbnail my script will fail. probably because after two have been done my script still thinks there are another 3 to go and if there is nothing to move/copy/whatever then it will fail.

 

why is this happening when there are only 2 files?

 

this is the testing code i am using:

<?php

$alId = $_POST['album'];
$image = $_FILES['image'];
$tmpImage = $image['tmp_name'];
$imgImage = $image['name'];

echo $alId . "<br />";
for ($i=0; $i < count($image); $i++) {
if(!empty($tmpImage)) {
	echo $tmpImage[$i] . " - " . $imgImage[$i] ."<br />";
}
}

?>

 

thanks in advanced!

Link to comment
https://forums.phpfreaks.com/topic/116012-solved-handling-multiple-file-uploads/
Share on other sites

your using count that returns 2 because the array have 2 items..

but the loop starts at 0, and ends at 2 thus 0,1,2 AKA 3

 

try

for ($i=0; $i < (count($image)-1); $i++) {

 

Another option

foreach($tmpImage as $K=> $img) //$K = array key, $img = current items
{
if(!empty($img)) {
	echo $tmpImage[$K] . " - " . $imgImage[$K] ."<br />";
//please note $tmpImage[$K] is the same as $img
//its just an example (normally i would use $img)
}
}

well the array starts at 0

 

and by the looks of, the reason the loop prints the extra '-' underneath is because my array seems to have 5 entires but only 2 are an actul string.

 

however the example you wrote works perfectly fine. thanks alot!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.