Multiple Image Upload

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

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

Multiple Image Upload

Post 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;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

each element of the $_FILES['filetoupload'] variable is an array now.. one for each file upload field I believe.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

no.

print_r($_FILES['filetoupload'])

you'll see what I mean.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

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

Post by s.dot »

haha I can?

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

I'm a bit on the uneducated side.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's a user defined function... it contains your upload processing code. :P
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

ah I gotchya!

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

Post 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]
?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

read the code I posted again.. :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

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

Post by s.dot »

well......
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

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