wrathican Posted June 16, 2008 Share Posted June 16, 2008 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++; } ?> Quote Link to comment Share on other sites More sharing options...
Jabop Posted June 16, 2008 Share Posted June 16, 2008 Firstly, I do believe php counts as each file, given the script will reprocess after each loop. Secondly, why don't you just increase the max upload and max post in you php.ini? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 16, 2008 Share Posted June 16, 2008 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 } } ?> Quote Link to comment Share on other sites More sharing options...
wrathican Posted June 16, 2008 Author Share Posted June 16, 2008 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? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 16, 2008 Share Posted June 16, 2008 no offense...but your while loop won't work and makes no sense Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted June 16, 2008 Share Posted June 16, 2008 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 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 16, 2008 Share Posted June 16, 2008 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 } } ?> Quote Link to comment Share on other sites More sharing options...
Jabop Posted June 16, 2008 Share Posted June 16, 2008 rhodesa: Why do you reassign the variables? For readability? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 16, 2008 Share Posted June 16, 2008 yes, purely readability/ease of use...it just makes the code easier to read Quote Link to comment Share on other sites More sharing options...
Jabop Posted June 16, 2008 Share Posted June 16, 2008 Do you practice that with your own programming? Reassigning variables is pointless Quote Link to comment Share on other sites More sharing options...
wrathican Posted June 16, 2008 Author Share Posted June 16, 2008 what exactly in my loop didn't make sense? i thought it made perfect sense i know i didnt move the ['tmp_name'] and maybe i missed out some ';' but i thought that would have worked otherwise Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 16, 2008 Share Posted June 16, 2008 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 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 Quote Link to comment Share on other sites More sharing options...
Jabop Posted June 16, 2008 Share Posted June 16, 2008 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 Quote Link to comment Share on other sites More sharing options...
wrathican Posted June 16, 2008 Author Share Posted June 16, 2008 ah i see about the infinate thing: would: while(!empty($_FILES['file'])) work? Quote Link to comment Share on other sites More sharing options...
Jabop Posted June 16, 2008 Share Posted June 16, 2008 his example: <?php foreach($_FILES['upload']['name'] as $n=>$name) { //do stuff } ?> will get the job done Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.