Login

Uploading Files and Navigating Directories in 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.”

Code

For file uploading to work, it must be turned on in your php ini file. And the upload _tmp_dir must be set; in other words the upload directory must exist.

Below is an extract from a php ini file:

;;;;;;;;;;;;;;;;

; File Uploads ;

;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.

file_uploads = On

; Temporary directory for HTTP uploaded files (will use system
default if not

Once you’ve made all the necessary changes to your php settings, let’s create the form needed to do the uploads. To enable a form to handle uploads, you need to change three things on a standard form.

First, the initial form line must include enctype=”multipart/form-data.” This tells the browser to expect different types of data.

Second, a hidden input type should be included to tell the browser the max file size allowed for uploading. This is not strictly necessary, as users may upload files of varying sizes.

Third, the “<input name=”userfile” type=”file”>” is included to create the upload field and also creates the browse button. Below is an example of a upload form.

Fig1. Upload form example

PHP fills a $_FILES[''] array with information about the file that you upload. The array contains the following info:

Name – name of the file as it was on the local disk.

Type – mime type of the file (example image/gif).

Size – file size.

Tmp_name- name of the file as it is stored in the final destination.

Error – error code for when something goes wrong.

Code

Meaning

0

No Error

1

File exceeds set upload_max_filesize

2

File exceeds the max_file_size setting in form

3

File was partially uploaded

4

No file was uploaded

{mospagebreak title=Uploading files}

When a file is uploaded, it is first placed in a temporary directory. You have to use the move_uploaded_file() function to move it to its final destination. The move_uploaded_file function takes three parameters:

This is where our files are going to be stored on our C drive. Next we check to see whether the Files array is filled:

‘ if($_FILES['userfile']['name']) {‘

If it is filled, the ‘move_upload_file()‘ function is used to upload the file and a message is shown. In case the array is not filled we define a switch statement with the various error numbers and what they mean:

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'][0] array will contain uploader.html and the $_FILES['Afile']['name'][1] 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(isset($_FILES['AFile']['error'])){ ‘

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.

{mospagebreak title=Navigating Directories}

Before we start with the next section, please make sure that you are familiar with file permissions. Pretty much everything in the File Permissions section applies to the directories. To navigate through a directory, you begin by opening the directory with opdir() function:

$dir = opendir(‘directoryname’);

As with reading and writing files, you create a pointer that can be referenced by the opened directory. To read the list of files within a directory you use the readdir() function, in a while loop:

while($thedir=readdir($dir)){//write the file details here}

and then you finally close the open directory with the closedir() function:

closedir($dir);

Other functions that we are going to use include:

filesize() function – gives you the size of the file in bytes.

filetime() – retrieves the modification time of a file.

Both of these functions are going to help us to create a directory listing.

Let’s build a script that will show us the insides of a directory listing: