In consonance with the concepts that I expressed in the prior section, the best way to demonstrate the capacity of the recently-defined “uploadFile()” function is by putting it to work in the context of a practical example. Therefore, suppose for a moment that I already coded a simple web form for letting users browse different files on their machines and uploading only one of them to the web server, like the one shown in the beginning of this tutorial.
As you know, the sample (X)HTML that contains this online form would look like this:
<!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>
Now that I've coded the above file uploading web form, the only thing that remains undone is defining the pertinent “upload_file.php” file. This time it will include the definition of the previous “uploadFile()” custom function that you saw before.
Given that, the aforementioned PHP file would now look like this:
// define 'uploadFile()' function
throw new Exception('Invalid upload directory.');
throw new Exception('Invalid number of file upload parameters.');
throw new Exception('Invalid MIME type of target file.');
// throw exception according to error number
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.');
// call 'uploadFile()' function
echo 'Target file uploaded successfully!';
Did you realize how easy was to build a file uploading application using the “uploadFile()” PHP custom function listed previously? I’m sure you did! In this case, only two source files are required to get this application working. These can be quickly integrated into other existing PHP programs.
All right, at this point, I've shown you how to create an extensible file uploading script that only uses a simple PHP custom function to work as expected. Of course, the script has plenty of room to be extended and improved, but this process, due to the intrinsic flexibility offered by the “uploadFile()” function, can be performed in an effortless way.
That’s all for the moment. In this fourth part of the series, I taught you how to implement a highly-expandable file uploading application by way of a straightforward PHP 5 function that encapsulates all the logic required to perform this task in a few simple steps.
And speaking of logic encapsulation, in the final tutorial of the series, I’ll show you how to construct a file uploading application similar to the one discussed before, but this time using a highly-modular PHP class.
So, if you’re a strong advocate of using the object-oriented paradigm with PHP 5, you simply can’t miss this last article!
blog comments powered by Disqus