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">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Uploading files with PHP</title>
font: bold 16pt Arial, Helvetica, sans-serif;
font: normal 10pt Arial, Helvetica, sans-serif;
border: 1px solid #666;
<h1>Uploading files with PHP</h1>
<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>
(definition of 'upload_file.php' file)
// set upload directory (for Windows users)
// set destination of uploaded file
echo 'The target file was successfully uploaded!<br />Name of uploaded file: '.$_FILES
// display error messages when file upload fails
throw new Exception('Target file exceeds maximum allowed size.');
throw new Exception('Target file exceeds the MAX_FILE_SIZE value specified on the upload form.');
throw new Exception('Target file was not uploaded completely.');
throw new Exception('No target file was uploaded.');
throw new Exception('Missing a temporary folder.');
throw new Exception('Failed to write target file to disk.');
throw new Exception('File upload stopped by extension.');
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.
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!
blog comments powered by Disqus