Home arrow PHP arrow Page 3 - Retrieving Information on Selected Files with a PHP 5 File Uploader

Retrieving useful information on the uploaded file - PHP

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.

TABLE OF CONTENTS:
  1. Retrieving Information on Selected Files with a PHP 5 File Uploader
  2. Reintroducing a previous hands-on example
  3. Retrieving useful information on the uploaded file
  4. Listing the complete source code of the improved file uploading application
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
March 26, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: