Jump to content

php multiple file upload form


wrathican

Recommended Posts

hey guys im thinking of creating an upload form for my users that can handle multiple files.

What i want to happen is there be one file input to begin with. when that input has a file selected display another one. i am aware that is done using javascript.

 

What i want from this forum is a few answers.

If the user selects 5 files to be uploaded and each file is 512Kb big then the TOTAL file size of all the files together would be 2.5Mb. If the PHP upload limit is 2Mb would this cause the script to fail? Or does it count each file selected as an individual file?

 

Can I use a while loop to process the upload?

e.g:

<?php

$file = $_FILES['file'];
$dest = "some/folder/";
//i realize i need to get the name but this is a quick example.

$i = 1
while(!empty($file)){
//process upload
$upload = move_uploaded_file($file, $dest);
if($upload) {
echo "File " . $i . " has been uploaded";
}else{
echo "File " . $i . " has not been uploaded";
}
$i++;
}

?>

 

Link to comment
Share on other sites

Not sure about the filesize limitations...but to have multiple uploads, just have the JS generate the tags like so:

<input type="file" name="upload[]" />

make sure they all have the same "name" and there are close brackets, then the PHP would look like:

 

<?php
  if(is_array($_FILES['upload']) && is_array($_FILES['upload']['name']){
    foreach($_FILES['upload']['name'] as $n=>$name){
      //$name is now set to the name
      $type = $_FILES['upload'][$n]['type'];
      $size = $_FILES['upload'][$n]['size'];
      $tmp_name = $_FILES['upload'][$n]['tmp_name'];
      $error = $_FILES['upload'][$n]['error'];
      //Normal file upload process using the above variables
    }
  }
?>

Link to comment
Share on other sites

well, thanks for the advice.

 

i just wanted to know whether or not i had to set the ini options at the beginning of my script. i feel better not having to since im not my host.

 

so is it best practice to do it the way you recommended rhodesa? is a while a bad idea?

Link to comment
Share on other sites

If the PHP upload limit is 2Mb would this cause the script to fail?

 

Depends.  There are two variables...post_max_size and upload_max_filesize.  The sum of all files can not exceed post_max_size.  No single file can exceed upload_max_filesize.

 

http://us.php.net/ini.core#ini.upload-max-filesize

http://us.php.net/ini.core#ini.post-max-size

 

Or does it count each file selected as an individual file?

 

See above.

 

Can I use a while loop to process the upload?

 

Yes

Link to comment
Share on other sites

just realize my code was a little off...updated:

<?php
  if(is_array($_FILES['upload']) && is_array($_FILES['upload']['name']){
    foreach($_FILES['upload']['name'] as $n=>$name){
      //$name is now set to the name
      $type = $_FILES['upload']['type'][$n];
      $size = $_FILES['upload']['size'][$n];
      $tmp_name = $_FILES['upload']['tmp_name'][$n];
      $error = $_FILES['upload']['error'][$n];
      //Normal file upload process using the above variables
    }
  }
?>

Link to comment
Share on other sites

Do you practice that with your own programming? Reassigning variables is pointless

if i was only using the value once, i probably wouldn't reassign it. if i had a complicated block of code where i was reusing the value over and over, i would probably reassign for a couple of reasons:

1) To reduce the chance of me typing the variable wrong. If I don't reassign, I find I'll leave the $n part off somethimes ($name is a lot easier then $_FILES['upload']['name'][$n])

2) Code readability

3) It's actually less characters if you use the variable more then once.

 

in things i do, the extra memory usage by storing the value of a few short strings into a new variable is not an issue

 

 

what exactly in my loop didn't make sense? i thought it made perfect sense :P

 

i know i didnt move the ['tmp_name'] and maybe i missed out some ';' but i thought that would have worked otherwise

 

your loop will be infinite. $file will never be empty because you never change the value of it

Link to comment
Share on other sites

Do you practice that with your own programming? Reassigning variables is pointless

if i was only using the value once, i probably wouldn't reassign it. if i had a complicated block of code where i was reusing the value over and over, i would probably reassign for a couple of reasons:

1) To reduce the chance of me typing the variable wrong. If I don't reassign, I find I'll leave the $n part off somethimes ($name is a lot easier then $_FILES['upload']['name'][$n])

2) Code readability

3) It's actually less characters if you use the variable more then once.

 

in things i do, the extra memory usage by storing the value of a few short strings into a new variable is not an issue

 

Touche. I can understand in that aspect, but I was moreso speaking of small scripts like the one shown

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.