Page 1 of 2

Multiple Image Upload

Posted: Fri Mar 04, 2005 4:15 am
by s.dot
Here's my form that displays 15 file fields.

Code: Select all

<form action="dcamset.php?password=password&camset=<? echo $camset; ?>" method="post">
<input type="hidden" name="action" value="adddpicture">
<? for($i=0; $i<15; $i++)&#123; ?>
<input type='file' name='filetoupload&#1111;]' size='20'><BR><? &#125; ?>
<input type="submit" value="Upload Pictures">
</form>
I need tips/suggestions on how to turn the following code into one that will iterate through the files uploaded until it is finished:

Code: Select all

if($action == "adddpicture")&#123;
$upload_dir = "digital/$camset/";
if(is_writable($upload_dir))&#123; echo "Is Writeable"; &#125; ELSE &#123; echo "Not Writeable"; &#125;
if(is_uploaded_file($_FILES&#1111;'filetoupload']&#1111;'tmp_name'])) &#123; 
$filename2 = $_FILES&#1111;'filetoupload']&#1111;'name'];
$extension = strtolower(strrchr($filename2,"."));
$extensions = array('.jpg','.jpeg','.png');
if(!in_array($extension, $extensions)) &#123; echo "File type not allowed."; &#125;
$rand = rand(0,9999999999);
$filename = $rand.$extension;
if(move_uploaded_file($_FILES&#1111;'filetoupload']&#1111;'tmp_name'],$upload_dir.$filename)) &#123; 
include 'createthumb.inc.php';
createthumb("digital/$camset/$filename","digital/$camset/thumbs/$filename",100,100);
$insertsql = "INSERT INTO digitalfiles (filename, camsetid) VALUES('$filename', '$camset')";
mysql_query($insertsql) or die(mysql_error()); &#125; ELSE &#123;
echo "There was a problem moving your file"; &#125; &#125; &#125;

Posted: Fri Mar 04, 2005 7:55 am
by feyd
each element of the $_FILES['filetoupload'] variable is an array now.. one for each file upload field I believe.

Posted: Fri Mar 04, 2005 8:11 am
by s.dot
Alright, so I now have $_FILES['filetoupload'][1] through 15

Code: Select all

for($_FILES&#1111;'filetupload'] = 0; $_FILES&#1111;'filetoupload'] < 15; $_FILES&#1111;'filetoupload']++)&#123;

// upload code here

&#125;
would work?

Posted: Fri Mar 04, 2005 8:13 am
by feyd
no.

print_r($_FILES['filetoupload'])

you'll see what I mean.

Posted: Fri Mar 04, 2005 3:05 pm
by s.dot
I understand what you mean. It will return an array with elements such as [size] [type] etc.

How will this help me iterate through the upload code?

Posted: Fri Mar 04, 2005 3:20 pm
by feyd

Code: Select all

$keys = array_keys($_FILES&#1111;'filetoupload']);
foreach($_FILES&#1111;'filetoupload']&#1111;'name'] as $n => $name)
&#123;
  $filedata = array();
  foreach($keys as $key)
    $filedata&#1111;$key] = $_FILES&#1111;'filetoupload']&#1111;$key]&#1111;$n];
  processFile($filedata);
&#125;
you can figure out what goes in processFile() ;)

Posted: Fri Mar 04, 2005 3:34 pm
by s.dot
haha I can?

I checked php.net for processFile
couldn't find it...

I'm a bit on the uneducated side.

Posted: Fri Mar 04, 2005 3:39 pm
by feyd
it's a user defined function... it contains your upload processing code. :P

Posted: Fri Mar 04, 2005 3:41 pm
by s.dot
ah I gotchya!

thanks much.

Posted: Fri Mar 04, 2005 3:47 pm
by s.dot
I'm not quite sure how I would do this

Code: Select all

function processFile($filedata)&#123;
// upload code here//
&#125;
processFile($_FILES&#1111;'filetoupload']&#1111;$key]&#1111;$n]
?

Posted: Fri Mar 04, 2005 3:50 pm
by feyd
read the code I posted again.. :)

Posted: Fri Mar 04, 2005 3:54 pm
by s.dot
yes I know

I would have this:

Code: Select all

$keys = array_keys($_FILES&#1111;'filetoupload']); 
foreach($_FILES&#1111;'filetoupload']&#1111;'name'] as $n => $name) 
&#123; 
  $filedata = array(); 
  foreach($keys as $key) 
    $filedata&#1111;$key] = $_FILES&#1111;'filetoupload']&#1111;$key]&#1111;$n]; 
  function processFile($filedata); &#123;
 //upload code// &#125;
processFile($_FILES&#1111;'filetoupload']&#1111;$key]&#1111;$n]);
&#125;
That's what I was getting at, I was just wondering if

Code: Select all

function processFile($filedata); &#123;
 //upload code// &#125;
processFile($_FILES&#1111;'filetoupload']&#1111;$key]&#1111;$n]);
that part was correct?

Posted: Fri Mar 04, 2005 4:04 pm
by feyd
nope.

Posted: Fri Mar 04, 2005 4:10 pm
by s.dot
well......

Posted: Fri Mar 04, 2005 4:14 pm
by Burrito
that function doesn't have anything in it and you're just calling it again within the function...

you need to make it do soemthing

research move_uploaded_file