Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Uploading an image, document or pdf via a browser (php)

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
H304 - PHP - DDA, Style and image handling
  [3747] An easy way to comply with the new cookie law if your site is well designed - (2012-06-02)
  [2917] Upload Image, Store in database, redisplay in browser. PHP and MySQL - (2010-08-12)
  [2539] Changing Images - (2009-12-17)
  [2343] World Flags in your PHP pages - (2009-08-10)
  [2031] Choosing from an image with an image map - (2009-02-08)
  [1506] Ongoing Image Copyright Issues, PHP and MySQL solutions - (2008-01-14)
  [806] Check your user is human. Have him retype a word in a graphic - (2006-07-17)

Q904 - Object Orientation and General technical topics - Analysing a Programming Task
  [3895] Flowchart to program - learning to program with Well House - (2012-10-14)
  [3461] From flowchart to program - code design for the newcomer - (2011-09-29)
  [3366] Specification, Design, Implementation, Testing and Documentation - stages of a (Java) programming project - (2011-07-21)
  [3329] Perl from basics - (2011-06-20)
  [2834] Teaching examples in Perl - third and final part - (2010-06-27)
  [2327] Planning! - (2009-08-08)
  [1853] Well structured coding in Perl - (2008-10-24)
  [1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
  [1607] Learning to program in Perl - (2008-04-11)
  [1513] Perl, PHP or Python? No - Perl AND PHP AND Python! - (2008-01-20)
  [1345] Perl and Shell coding standards / costs of an IT project - (2007-09-11)
  [747] The Fag Packet Design Methodology - (2006-06-06)


Back to
A simple example - XML from a Ruby program
Previous and next
or
Horse's mouth home
Forward to
Melksham in Pictures
Some other Articles
Traffic lights in Python
Python - access to variables in the outer scope
The Multiple Inheritance Conundrum, interfaces and mixins
Melksham in Pictures
Uploading an image, document or pdf via a browser (php)
A simple example - XML from a Ruby program
History is all around us
A more informed decision than ever before
For loop - checked once, or evety time? Ruby v Perl comparison and contrast
__index and __newindex in Lua - metatable methods
4300 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2715_Upl ... -php-.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat