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

Listing the complete source code of the improved file uploading application - 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

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!



 
 
>>> 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: