![]() Join Up! 96812 members and counting! |
|
|||
File uploads made easy
Darren Beale
Overview:
Every time I've written some code to upload a file, either to send it off as an email
attachment or as an image for some dynamic content piece, I've always meant to write
a few functions so I don't have to write the code again.
Well, people on the Back-end.org support board have
been asking for the ability to add images to news and articles recently, so I
*finally* sat down and wrote this script in order that I can add that functionality
to the next release.
To make things more interesting and to make it truly re-useable, I'm going to make
it totally generic so we can use it again and again for differing file types.
If you want the code to look @ in your favourite editor whilst you are reading this
piece, you can download it here.
Starting us off, we need to lay down some parameters, for the maximum file size you
want to allow the user to upload and - if it's an image - how high and wide the image can be, else you might get a 1.5MB, 1024 x 768 Jpeg filling up the entire screen!
Next is the path where the resulting file will sit on your server, this should be absolute and PHP needs 'write' permissions to that directory.
Now, we want to list a number of file-types so we can give sensible error messages later on in the script.
This is simply an array indexed by 'content-types' with a value that is human readable
Finally, we want to specify what type of file the user is allowed to upload, for this example, it's an image that we might want to embed in a web page, so we build up an array of related 'image' content-types.
An OO developer would probably make this whole script a class and have each one of the following functions as methods. Personally, I prefer to write my code using a function based approach.
To start us off, I want to write the upload form as a small function, I can then call it many times in this code without lots of messy repeating. If you are wondering why there are lots of \n's everywhere, this is so that the outputted HTML is not all squashed onto one line. In bigger applications it makes for easier debugging when you view source, so I'm not being anal for no reason.
Note, the <form enctype..> if we don't add this nothing will get uploaded.
I've also set the parameter $error to false, this is a nice way of pre-defining the value, so I don't have to always pass the parameter in.
Our second function is to check if a certain value is in an array, now PHP4 has this built in, but PHP3 does not, so we wrap an IF statement around it checking the PHP version, if you are running PHP4, you can delete the whole thing but I've left in so it can use it on differing servers without too much thought.
The main function in this script is for the validation of our upload, here we do things like check the file type, and if an image, what it's dimensions are.
Notes:
This next function isn't really needed, but I've put it in so our user gets some confirmation that the file was uploaded, all it does is loop through the upload directory and print out the files in it, except for '.' and '..'
Our last function brings all of our functions together. It takes the file as input and then uses validate_upload() to check that it is allowed. If it is then we need to copy it from the temp directory (this is specified in php.ini) and move it to it's final resting place.
One last sanity check is to see that we can copy to that directory. If this falls over it is almost certainly a permission problem or an invalid path.
If everything is OK, then we call list_files() and then re-display the form.
Our last bit of code now is to kick things off. We use the case() statement to switch between tasks. It's a slight overkill here, but I've left it in so you can see how it might be used, for larger projects it makes for easily readable code, though some developers might disagree with me.
If $task = upload then the upload process begins, otherwise as a default we just show the form
That's it, upon running this script the user will see just a form with an upload field, they browse the hard-drive for a file and click submit. If there are no errors in the validation stage, they will get returned a directory listing of the upload folder and the form again so they can upload another file.
-- Bealers
Notes:
I am well aware that I've added no error checking for the max_file_size, AFAIK the only way to gracefully capture this error is to turn all error_reporting off in php.ini and do code based checking. However I like to see my errors for development, so I guess we are stuck with the messy PHP error that is generated.
This example does not show you what to do with the image once it is uploaded. For example, as I will be for the next release of Back-end, you might want to allow a user to upload an image for a news article. In this case you would possibly add the file name to the article so when the page is viewed, php can pull the corresponding image onto the page. I'll leave you to figure that one out =)
|