Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Handling file-uploads with PHP

Thursday, January 12th, 2017 by Servage

Upload-FilesSending files with an HTML form is quite common on websites these days. When using a help desk, you can often attach a screenshot of the issue to help customer service assist you easier. When applying for a job online, you can attach your CV to the application. In both of these cases, the attached files are processed by a server and saved to a specific location. Let’s see what it takes to build a form like this with file uploading capabilities using PHP.


Let us first get started with the required HTML. All we need is a typical form with a file input field. The input field can look like this: <input type=”file” name=”file”>. The name attribute will be used to find the uploaded file using PHP. A submit button is needed too, and you can create one using the <input type=”submit”> element. Because the default HTTP method for a form is GET, the form’s method should be set to POST: <form method=”post”>.

The $_FILES superglobal

When the form is submitted, all files can be found in the $_FILES superglobal variable. $_FILES is an array where one one file equals one member of the array. Because we set the name attribute of the file input to “file”, the file can be found at $_FILES[“file”]. If you don’t know what name attribute the form uses or if there are multiple files, you can loop through the files using a foreach loop.

Actually, the $_FILES is a multidimensional array. This allows us to get some basic information of the files in the array. For example, to get the name of the file, we can use $_FILES[“file”][“name”]. To get the size of the file, use $_FILES[“file”][“size”].

Processing uploaded files

Now that we have access to the uploaded file, we can go ahead and process the upload. The first thing is to ensure that the file is valid. For example, if we want to only accept files up to 1 MB in size, we can check this using the following if statement: if ($_FILES[“file”][“size”] <= 1000000). The size here is in bytes. If you want to allow only specific file types, you can use the pathinfo() function to get the file extension.

When all validations are done, the last step is moving the file to its destination folder. This can be done with the move_uploaded_file() function. Here is an example: move_uploaded_file($_FILES[“file”][“tmp_name”], “/tmp/” . $_FILES[“file”][“name”]). The first argument is the current location of the file and the second is the new location. Note that this time we used “tmp_name” instead of “name” in $_FILES. “tmp_name” is the file’s temporary name on the server while “name” is the name of the file on the uploader’s computer.

After calling the move_uploaded_file() function, the file can be found in the specified target location, in this case under the system’s temporary folder.

Handling file-uploads with PHP, 5.0 out of 5 based on 2 ratings
Categories: Tips & Tricks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.