HomePHP Defining a Custom Function for File Uploaders with PHP 5
Defining a Custom Function for File Uploaders with PHP 5
In the next few lines I’m going to show you how to wrap the file uploading application built in the previous article of the series into a single custom PHP function. This will turn it into a more maintainable and reusable piece of code.
Learning how to implement file uploads with PHP is undoubtedly one of those things that you have to tackle sooner or later, during your life as PHP developer. And if you’re not armed with a decent background on this topic, there’s the possibility that by the time you finish building your first file uploading application, you will have suffered serious hair loss.
However, there’s no need to expect this, since in this series of articles, you’ll find the right pointers to help you start implementing file uploads with PHP 5. And as always, you’ll be provided with copious practical examples that will help you understand this useful topic from a hands-on point of view.
Now that you’ve been introduced to the principal subject of this article series, let me quickly rehash the items that I discussed in the preceding installment, in case you haven’t read it yet. In the course of the mentioned tutorial, I went through the development of a basic error handling module that came in handy for keeping track of all the errors that might occur during a specific file uploading process.
What’s more, the logic implemented by this simple error checking module was built around the functionality provided by the $_FILES super global PHP array, since it uses one of its elements to store different error codes that are associated with a particular file uploading failure. In this way, it was pretty simple to instruct the module in question to throw several exceptions that could be neatly caught by a typical “try-catch()” block.
So far, so good. At this point, I should assume that building an error checking mechanism that is aimed at tracking all the failures that might arise when uploading a selected file to a web server, is now a familiar process to you. Therefore, it’s time to introduce a few other improvements to the pertinent file uploading application that you learned in the previous tutorial.