HomePHP Page 2 - Building File Uploaders with PHP 5
Implementing file uploads with PHP: the $_FILES superglobal array - PHP
My goal here is simply tackling file uploads from a hands-on point of view and discarding all of those additional configuration settings that must be performed in the web server. Therefore, in this series of articles, Iím going to show you how to build some file uploading mechanisms with PHP 5, which, due their intrinsic modularity, can be easily integrated into other existing PHP applications with minor hassles.
It's practically impossible to cover file uploads in PHP 4/5 without examining one of the key elements involved in this process in detail. Yes, you guessed right! In this case, I'm talking about the $_FILES superglobal PHP array (or its equivalent in earlier versions, $HTTP_POST_FILES), which handles every parameter created by PHP with precision, such as when a file is being uploaded to the server via the HTTP protocol.
But it's time to get rid of this rather boring theory and see how the $_FILES array stores all the information related to an uploaded file. The following list shows each element that makes up this array along with a brief description of what it does (notice that the explanations assume that the web form used to upload a file contains a field named "userfile"):
$_FILES['userfile']['name'] (Specifies the original name of the file being uploaded on the client computer).
$_FILES['userfile']['type'] (Specifies the MIME type of the file being uploaded, for instance, "image/jpeg").
$_FILES['userfile']['size'] (Indicates the size in bytes of the file being uploaded).
$_FILES['userfile']['tmp_name'] (Indicates the temporary name used by the web server to store the uploaded file).
$_FILES['userfile']['error'] (Specifies the error code associated with a specific file upload).
Now that I have listed the elements that make up the $_FILES superglobal array, you can see more clearly, based on the above descriptions, that PHP provides all the parameters required to perform file uploads with minor hassles.
It's important to stress that the array in question also includes an element that stores an error number associated with the file uploading process, which means that it's pretty simple to implement the logic necessary to handle these eventual errors. But I'm getting ahead of myself, since this topic will be covered in an upcoming tutorial of the series.
So, at this point, I have provided you with a quick overview of the different elements included in the $_FILES superglobal array, which stores all the information related to a specific file uploading process. But you're probably wondering how this array can be used to perform a file upload within a PHP script, right?
Well, first it's necessary to build a simple front-end that permits users to select a particular file in the client machine, and then upload it to the web server. Thus, considering this requirement, in the section to come I'm going to show you how to create a simple web form that will perform this task in a straightforward way.
Naturally, to see how this file uploading form will be coded, you'll have to click on the link below and keep reading.