HomePHP Page 3 - Uploading Files and Navigating Directories in PHP
Multiple file uploads - PHP
In this article we are going to look at how to upload files and also how to navigate through directories. It is the second part of a tutorial that began last week with "Reading, Writing, and Creating Files in PHP."
When the above form is submitted, the arrays $_FILES['Afile'], $_FILES['A']['name'], and $_FILES['Afile']['size'] will be initialized and all of them will be numerically indexed. For example say you submit three files with the names uploader.html, fileprocess.php and what.php. The $_FILES['Afile']['name'] array will contain uploader.html and the $_FILES['Afile']['name'] array will contain what.php, and so on. All of the file attributes such as size and name will be available for each file that you submitted.
Once you've submitted the form you simply do a "for each" loop and display the form information.
Another way in which you can upload multiple files is by explicitly giving the "name" part of the form input a different name:
As with our previous upload script, first we define where we want to store the uploaded files by declaring '$uploaddir="c:uploads";' From this declaration we can see that our files are going to be stored on our C drive. Next we check whether there are any errors in the submitted File array:
If there are any errors, the files will not be uploaded. If on the other hand there are no errors, the files are uploaded by the 'move_upload_file()' function.
There's another way that you can dynamically create input fields on a form to upload files. If you present a user with a form asking how many files he or she wants to upload, once you have the number you can simply create a dynamic form with n number of input tags and continue from there.