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

Reintroducing a previous hands-on example - 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

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.



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