One 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
(written 2010-04-10)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q904 - Object Orientation and General technical topics - Analysing a Programming Task [747] The Fag Packet Design Methodology - (2006-06-06)
[1345] Perl and Shell coding standards / costs of an IT project - (2007-09-11)
[1513] Perl, PHP or Python? No - Perl AND PHP AND Python! - (2008-01-20)
[1607] Learning to program in Perl - (2008-04-11)
[1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
[1853] Well structured coding in Perl - (2008-10-24)
[2327] Planning! - (2009-08-08)
[2834] Teaching examples in Perl - third and final part - (2010-06-27)
[3329] Perl from basics - (2011-06-20)
[3366] Specification, Design, Implementation, Testing and Documentation - stages of a (Java) programming project - (2011-07-21)
[3461] From flowchart to program - code design for the newcomer - (2011-09-29)
[3895] Flowchart to program - learning to program with Well House - (2012-10-14)
H304 - PHP - DDA, Style and image handling [806] Check your user is human. Have him retype a word in a graphic - (2006-07-17)
[1506] Ongoing Image Copyright Issues, PHP and MySQL solutions - (2008-01-14)
[2031] Choosing from an image with an image map - (2009-02-08)
[2343] World Flags in your PHP pages - (2009-08-10)
[2539] Changing Images - (2009-12-17)
[2917] Upload Image, Store in database, redisplay in browser. PHP and MySQL - (2010-08-12)
[3747] An easy way to comply with the new cookie law if your site is well designed - (2012-06-02)
Some other Articles
Traffic lights in PythonPython - access to variables in the outer scopeThe Multiple Inheritance Conundrum, interfaces and mixinsMelksham in PicturesUploading an image, document or pdf via a browser (php)A simple example - XML from a Ruby programHistory is all around usA more informed decision than ever beforeFor loop - checked once, or evety time? Ruby v Perl comparison and contrast__index and __newindex in Lua - metatable methods