Retrieving Information on Selected Files with a PHP 5 File Uploader

The initial script in the first tutorial of this series lacked some important features, such as the implementation of an effective error handling module and the ability to check the MIME type and size of the file being uploaded. In this second installment of the series, I’m going to improve the logic of the script from the first tutorial to provide it with the capacity to retrieve useful information concerning the entire file uploading process.

As you know, nowadays the web has plenty of sites that provide visitors with the ability to easily upload their files to a web server. And this phenomenon, for bad or good, is a crucial part of the so-called “social web.” But regardless of what you may think of these kinds of websites, in all these cases, there’s a backend application that performs these file uploads via the HTTP protocol.

However, there’s no need to build a huge social website to implement a file uploading mechanism. And you, as a PHP developer, are probably well aware of this. Handing file uploads with PHP is one of the most popular topics to learn these days, and that’s the reason I decided to write this article series. In its different tutorials, you’ll learn the basic concepts required to build a file uploading application with PHP 5, first by using a procedural approach and then via the object-oriented paradigm.

Now that I’ve introduced you to the main subject of this group of articles, I’d like to quickly refresh the concepts that I deployed in the first part of the series, in case you didn’t have the chance to read it. In simple terms, I explained how to create a basic file uploading script in PHP 5 by using a combination of the “move_uploaded_file()” PHP native function and the $_FILES superglobal array.

Naturally, first it was necessary to construct a simple front-end composed of a typical web form that provides users with an intuitive mechanism to browse files in the client machine, then select one of them, and finally upload it to the server via the aforementioned PHP script. However, this was only an introductory approach for building a file uploader with PHP 5.

As you know, small moves can go a long way, so let’s continue learning how to handle file uploads with PHP 5. Let’s get going!

{mospagebreak title=Reintroducing a previous hands-on example}

An excellent way to demonstrate how to improve the logic of the file uploading script developed in the preceding article of the series is to list the respective definitions of its two source files, so you can recall how they were created on that specific occasion.

That being said, here’s is how the mentioned source files looked, so study them in detail:

(definition of ‘upload_form.htm’ file)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Uploading files with PHP</title>

<style type="text/css">

body{

padding: 0;

margin: 0;

background: #fff;

}

h1{

font: bold 16pt Arial, Helvetica, sans-serif;

color: #000;

text-align: center;

}

p{

font: normal 10pt Arial, Helvetica, sans-serif;

color: #000;

}

form{

display: inline;

}

#formcontainer{

width: 50%;

padding: 10px;

margin-left: auto;

margin-right: auto;

background: #eee;

border: 1px solid #666;

}

</style>

</head>

<body>

<h1>Uploading files with PHP</h1>

<div id="formcontainer">

<form enctype="multipart/form-data" action="upload_file.php" method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="20000" />

<p>File to upload <input name="userfile" type="file" />

<input type="submit" name="send" value="Upload File" /></p>

</form>

</div>

</body>

</html>



(definition of ‘upload_file.php’ file)


if($_POST['send']){

// set upload directory (for Windows users)

$uploadDir=’C:uploaded_files';

// set destination of uploaded file

$uploadFile=$uploadDir.basename($_FILES['userfile']['name']);

if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadFile)){

echo ‘The target file was successfully uploaded!';

}

else{

echo ‘Error uploading target file!';

}

}


As I explained before, the above file uploading application is comprised of two basic source files. The first one is responsible for creating a basic file uploading web form and the last one takes up the file selected by the user and implements the logic necessary for moving it from a temporary directory in the web server to its final location. In this case, the upload directory is located at C:uploaded_files, and it’s only valid for Windows users, but this option can be changed to work with a different operating system.

So far, so good. At this point, I will assume that you’re familiar with the source code that I used to perform file uploads with PHP. Thus, it’s time to see how to introduce some modifications to the previous script.

Given that the $_FILES superglobal array provides complete information on a file uploading operation, I’m going to use some of its additional elements to display accurate data about this process, including the size and MIME type, the size of the file being uploaded, and so forth.

To see how these minor improvements will be incorporated into the prior file uploading application, jump into the next section and keep reading.

{mospagebreak title=Retrieving useful information on the uploaded file}

As you probably know, the $_FILES superglobal array available in PHP allows you to retrieve all the information related to a specific file uploading process, including the MIME type and size, its temporary denomination, etc. Thus, based upon the capabilities of this useful array, I’m going to modify the script that I built in the previous article of this series so it can use the array in question to display detailed information about a particular file upload on the browser.

That being explained, please take some time to examine the signature of the improved file uploading script, which now looks like this:


// basic example on uploading a file to the server via HTTP and displaying information on it

if($_POST['send']){

// set upload directory (for Windows users)

$uploadDir=’C:uploaded_files';

// set destination of uploaded file

$uploadFile=$uploadDir.basename($_FILES['userfile']['name']);

if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadFile)){

echo ‘The target file was successfully uploaded!<br />';

echo ‘Name of uploaded file: ‘.$_FILES['userfile']['name'].’.<br />';

echo ‘MIME type of uploaded file: ‘.$_FILES['userfile']['type'].’.<br />';

echo ‘Size of uploaded file: ‘.$_FILES['userfile']['size'].’ bytes.<br />';

echo ‘Temporary name of uploaded file: ‘.$_FILES['userfile']['tmp_name'].'<br />';

}

else{

echo ‘Error uploading target file!';

}

}


As you can see, understanding the logic implemented by the above file uploading script is actually a no-brainer process that can be tackled with minor problems. In this specific case, the script uses a few elements included into the previously mentioned $_FILES PHP array in order to retrieve useful information about a particular file uploading process.

What’s more, once a targeted file has been successfully uploaded to the web server, the script in question echoes all the data related to this procedure to the browser, including the client name of the pertinent file and other relevant details, such as its MIME type and size in bytes, and its temporary name as well.

Therefore, based on the recently improved logic of the previous file uploading script, it’s quite easy to retrieve detailed information on a specific file. For instance, say that I just uploaded a sample “test.doc” file to the web server, which would produce the following result:


The target file was successfully uploaded!

Name of uploaded file: test.doc.

MIME type of uploaded file: application/ms-word.

Size of uploaded file: 19968 bytes.

Temporary name of uploaded file: C:uploaded_filesphp135.tmp


That was pretty useful, right? I’m not saying that the $_FILES superglobal array is going to change your life as a PHP developer, but you must admit it works pretty well when it comes to getting meaningful information regarding a specific file uploading process.

All right, at this point I’ll assume that you’ve already grasped how to use the aforementioned $_FILES PHP array to display all the data related to a particular file upload. So, what’s the next step to take?

Well, in the last section of this tutorial, I’m going to list the complete source code of this improved file uploading PHP application, including the corresponding upload web form and the modified script that you learned a few lines before so you can copy it and paste it directly into your code editor for testing purposes.

As I said earlier, this process will be done in the following section, so jump forward and keep reading.

{mospagebreak title=Listing the complete source code of the improved file uploading application}

In consonance with the concepts deployed in the prior section, below I listed the two source files that make up this PHP-driven file uploading application, including the pertinent web form and the script that I modified earlier.

That being said, here’s how these source files look:


(definition of ‘upload_form.htm’ file)


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Uploading files with PHP</title>

<style type="text/css">

body{

padding: 0;

margin: 0;

background: #fff;

}

h1{

font: bold 16pt Arial, Helvetica, sans-serif;

color: #000;

text-align: center;

}

p{

font: normal 10pt Arial, Helvetica, sans-serif;

color: #000;

}

form{

display: inline;

}

#formcontainer{

width: 50%;

padding: 10px;

margin-left: auto;

margin-right: auto;

background: #eee;

border: 1px solid #666;

}

</style>

</head>

<body>

<h1>Uploading files with PHP</h1>

<div id="formcontainer">

<form enctype="multipart/form-data" action="upload_file.php" method="post">

<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />

<p>File to upload <input name="userfile" type="file" />

<input type="submit" name="send" value="Upload File" /></p>

</form>

</div>

</body>

</html>



(definition of ‘upload_file.php’ file)

if($_POST['send']){

// set upload directory (for Windows users)

$uploadDir=’C:uploaded_files';

// set destination of uploaded file

$uploadFile=$uploadDir.basename($_FILES['userfile']['name']);

if(move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadFile)){

echo ‘The target file was successfully uploaded!<br />';

echo ‘Name of uploaded file: ‘.$_FILES['userfile']['name'].’.<br />';

echo ‘MIME type of uploaded file: ‘.$_FILES['userfile']['type'].’.<br />';

echo ‘Size of uploaded file: ‘.$_FILES['userfile']['size'].’ bytes.<br />';

echo ‘Temporary name of uploaded file: ‘.$_FILES['userfile']['tmp_name'].'<br />';

}

else{

echo ‘Error uploading target file!';

}

}


There you have it. With the two source files listed above at your disposal, you shouldn’t have major problems developing your own PHP file upload application, even though its current state is quite primitive.

However, if you’re learning the basic concepts of how to implement a simple file uploading mechanism with PHP, the entirety of the code samples included in this tutorial should be good enough to fit your needs for now.

Final thoughts

In this second chapter of the series, you learned how to use the $_FILES superglobal array included with PHP in order to create a simple file uploading application that is also capable of displaying relevant information about an uploaded file.

As you saw earlier, utilizing this useful array isn’t rocket science. So in consequence, you shouldn’t have major problems understanding how to use it when building your own file upload applications.

In the next tutorial of the series, things will become more interesting because I’m going to teach you how to incorporate an effective error handling module to the previous PHP script, which will come in handy when tracking the eventual failures that might occur during a particular file uploading process.

Now that you’re aware of the topics that will be discussed in the upcoming article, you won’t want to miss it!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan