Uploading an image, document or pdf via a browser (php)
Archive - Originally posted on "The Horse's Mouth" - 2010-04-10 14:34:28 - Graham EllisOne of the most popular scripts on our web site is a demonstration, with full source code, of how you can upload data from a browser, store it into a MySQL database, pull it back from that database, and display it within a new or future response pages.
But there's a lot of steps in that upload - store - download process, plenty of places where the server's configuration can trip up the person implementing it - database names, public writeable directories, character protection, PHP configuration, installation of MySQL - all need to be right for the thing to run. The net result is that we get a number of accolades saying "thank you for putting this working examples together" and a few brickbats saying "I cut and pasted your code and it didn't work".
To help those people for who it didn't work, we've added tips onto the demonstration page as to how to configure the thing - and that's been a big help. But there's still a requirement to help people who push a file into one end, have it fail to come out at the other, and ask "what now?"
It doesn't work ... what now?
The answer to "What now" is ... "split the application down into a series of shorter pieces that you can test, and try them one by one. That will help you isolate where the problem lies."
I've placed onto our web site a new script - [here] - which tests out the first phase. You can upload a file through this script, save it into a local temporary file on the server, and then report on its size and date. Checking that the size is identical to the size of the file you uploaded is a pretty good confirmation that it's the same file. You can run that script [here] - though please do not be too disappointed; for security reasons, I can't echo back the contents of the file most recently uploaded ... we would be providing an open invitation to spam advertisers!
The original article that cover the whole upload and download process is [here], and there's a specific update as to how it works with .pdf files [here].
So what if the upload demo fails?
1. Is your PHP configured to allow uploads?
2. Does the temporary folder exist (it won't by default on Windows)?
3. Did you try to upload more than the maximum file size?
4. Did you remember the unusual encoding type on your form?
5. Does the target directory exist
6. Does the web server daemon have write permission to the target directory for the file move?
The next steps
Once you have found which of the six reason(s) applied in your case, and you have the upload working, you can go on to the next stage of saving into a database. Remember things like the need to add appropriate \ protection, to log in to the database, to use a database and a table that are set up and waiting for the data.
Save to database, then retrieve, and make sure that - once again - you have identical data.
The final step is to have your script send out the correct response. Remember that you'll need a call to header to output the correct Mime type to the browser, and that the header call MUST come before any content at all. Remember too that any error or warning messages that your PHP injects into a binary stream will simply corrupt that stream, and cause your browser to show an error display, rather than the message.
We cover these subjects on Intermediate PHP for the leisure user and on PHP Techniques