Home arrow PHP arrow Page 4 - Creating an Error Handling Module for a PHP 5 File Uploader

Listing the complete source code of the improved file uploading application - PHP

Welcome to the third chapter of the series “Building file uploaders with PHP 5.” Comprised of five approachable tutorials, this series teaches you the fundamentals of constructing file uploading applications with PHP 5. It also complements the corresponding theoretical concepts with numerous hands-on examples. In this third tutorial of the series, I’m going to show you how to incorporate a brand new error checking module into the file uploading PHP script created in the last article.

TABLE OF CONTENTS:
  1. Creating an Error Handling Module for a PHP 5 File Uploader
  2. Performing file uploads without checking errors
  3. Creating an effective error handling module
  4. Listing the complete source code of the improved file uploading application
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
April 02, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I promised in the section that you just read, I listed the complete definitions corresponding to the two source files that comprise this basic file uploading PHP 5 application. Here they are:


(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)


try{

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 />Name of uploaded file: '.$_FILES
['userfile']['name'].'.<br />MIME type of uploaded file: '.$_FILES['userfile']['type'].'.<br />Size of
uploaded file: '.$_FILES['userfile']['size'].' bytes.<br />Temporary name of uploaded file:
'.$_FILES['userfile']['tmp_name'];

}

else{

// display error messages when file upload fails

switch ($_FILES['userfile']['error']){

case 1:

throw new Exception('Target file exceeds maximum allowed size.');

break;

case 2:

throw new Exception('Target file exceeds the MAX_FILE_SIZE value specified on the upload form.');

break;

case 3:

throw new Exception('Target file was not uploaded completely.');

break;

case 4:

throw new Exception('No target file was uploaded.');

break;

case 6:

throw new Exception('Missing a temporary folder.');

break;

case 7:

throw new Exception('Failed to write target file to disk.');

break;

case 8:

throw new Exception('File upload stopped by extension.');

break;

}

}

}

}

catch(Exception $e){

echo $e->getMessage();

exit();

}


That's all for now. With the two above source files at your disposal, you have the proper material to build your own file uploading application in PHP 5. Of course, there's plenty of room to improve the business logic of this still immature file uploader, but this can be a neat challenge for your programming skills.

Final thoughts

In this third part of the series, I showed you how to improve the file uploading application built during the two preceding articles by incorporating an effective error handling module into it. It can be easily customized to suit your personal needs.

Yet the application in its current incarnation still looks pretty basic. But there's no reason to feel down. In the upcoming tutorial, I'll be demonstrating how the complete source code of this file uploader can be encapsulated within a user-defined PHP function. This way it becomes reusable over time.

Don't miss the next article!



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