Before I show you how to create a reusable custom PHP function based on the file uploading script developed in the previous chapter of the series, first I’d like to list its full source code, so that you can recall how it worked as a standalone module.
Please take a quick look at the following pair of source files, which, as I said before, comprised the primary structure of the file uploading application previously created:
(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['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'];
// 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.');
As you’ll surely recall, the source files listed above comprise the core of the extensible file uploading application built in the preceding article of this series. Of course, the most important point to stress here, with reference to the logic implemented by the prior script, is its ability to throw a certain number of exceptions in accordance with all the potential errors that might arise after uploading a particular file to the web server.
The way that I constructed this error checking mechanism is quite simple to follow. Thus, I'm guessing that you shouldn’t have major problems grasping its underlying logic.
So far, so good. Now that you've recalled how the file uploading application looked originally, it’s time to see how it can be encapsulated into a custom PHP function, with the purpose of turning it into a reusable and compact piece of code.
Sounds pretty interesting, right? However, to see the full details on how this will be done, you’ll have to click on the link below and read the following section.
blog comments powered by Disqus