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++; } ?> Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/ 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? Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566733 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 } } ?> Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566737 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? Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566762 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 Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566765 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 Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566767 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 } } ?> Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566773 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? Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566777 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 Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566780 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 Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566781 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 Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566782 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 Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566790 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 Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566797 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? Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566801 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 Link to comment https://forums.phpfreaks.com/topic/110467-php-multiple-file-upload-form/#findComment-566802 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.