I want to build a web page where users can upload several files at once. The files will be images only - JPG, JPEG, PNG and GIF formats.
I’ve been looking around online and most solutions use Flash. I really don’t want to use Flash because Flash 10 broke a lot of the common multifile upload methods.
My idea is to create multiple file input fields dynamically. Each field would have its own browse button. Then there would be one upload button at the end to submit everything. Using JavaScript to add new input fields is easy enough.
I’m not sure about the PHP side though. Should I give all the file input fields the same name attribute? That way I could handle them with one PHP script. Or is there a way for PHP to automatically detect how many files were uploaded? Maybe I could put the file processing code in a loop.
What’s the best approach for handling multiple file uploads on the server side?
Check your upload_max_filesize and post_max_size in php.ini. I hit this exact problem - small test images worked fine, but when users uploaded multiple large photos, everything failed silently. PHP just truncates $_POST and $_FILES arrays without any warning when you hit these limits. Bump up those values or add client-side validation to catch oversized files before upload. Use ini_get() to check your current limits and show proper error messages when uploads fail. PHP’s defaults are way too low for multiple image uploads.
The dynamic input approach works well, but here’s something else to watch out for. When you create multiple file inputs dynamically, you need to handle the $_FILES array structure correctly on the PHP side. Each file in $_FILES[‘files’] gets its own array indices for ‘name’, ‘type’, ‘tmp_name’, ‘error’, and ‘size’. You’ll iterate through these parallel arrays instead of treating each file as a single object. Also, validate file types and sizes before processing - I learned this the hard way when users uploaded massive files that crashed my script. Set limits in both your HTML and PHP validation or your upload system will break.
i did the same thing! just name your inputs files[], and php will treat it as an array. then, you can just loop through $_FILES['files'] to handle each file. way simpler than counting uploads for sure!
This topic was automatically closed 6 hours after the last reply. New replies are no longer allowed.