Handling File & Image UploadsBy Peter McNulty : 18-12-2004
4.30 (70)
Multiple File Uploads
There are two methods of allowing multiple file uploads. One is to create many file input fields all with different names and the other is to create an array of file input fields. The latter is much more preferred as, it allows us to easily allow for unlimited number of file uploads.
We now have a new form to allow for these multiple file uploads: Multiple File Upload <html> And it looks like this: From looking at the code, you can see the 'array' of file input fields. We simply name them as we would name array elements in PHP. Now, we process the files just as we did before, however, as we have an array, we must refer to the index whilst we are uploading Hence, we must have a loop to look over all the elements in the array, and if they aren't empty, then complete the upload. Final Code <html> Not to difficult, is it? All we have changed, is we now have a for-loop which will loop over all 5 of the possible uploaded files, and then it will move the uploaded file to the correct directory. Every time we access $_FILES['ourFiles'] now, we must add [$i] to get the correct index of the array. To end this article, just a quite note that if you now wanted to allowed 10 file uploads, all you have to do is add 5 more file fields to your form. The script already has the capability to handle unlimited uploads if you want. Although it is recommended that you restrict this using a variable such as $maxUploads = 10 and check this against the count of the array. I hope this article helps you to create a more dynamic, PHP based site! << Page 3 of 3 Build Your Own Database Driven Website Using PHP & MySQL |
Ads
|