Main Content

Uploading to a MySQL database through PHP - examples and common questions

Archive - Originally posted on "The Horse's Mouth" - 2008-03-02 07:28:37 - Graham Ellis

One of the most popular pages on the technical part of our web site relates to the uploading of images to a server from a local disc file via a browser, then storing those images on a database, and displaying them later. Another popular page relates to the same thing with .pdf file - and really the two requirements are the same technically - a piece of data that's to be treated as an indivisible block by the server - a "blob" or binary large object in database terms - is to be uploaded, stored are then returned back later.

We have two examples that I'll point you at. The first does the whole thing in one program, and is documented in our solution centre. It's here and the demonstration - in this case - uses .jpg image files. The second example has been split into three parts which makes it much easier for the newcomer to take in - an initial form, then a script to store the binary data in a database and finally a script to return something from the database. This example uses a .pdf file.

We've even provided facilities for you to try out our scripts - see here for the image uploader and there's another script to search the image database based on the picture description here. The .pdf uploader demo starts here.

You'll find a whole load of user comments on the bottom of the .jpg image script, but I still get a lot of questions relating to how it works. That's no big surprise really - the script is quite short, but it makes use of a lot of PHP and MySQL facilities that aren't covered on many courses on the subject (as this is an example from our course you can be assured we'll cover it if relevant to even one member of the group. And many of the questions are about failing uploads - again, no big surprise as in order for our script yo work, file upload needs to be configure "in" on the PHP server, a temporary directory assigned as appropriate as it probably won't be there by default on windows boxes, file permissions need to be set, a database needs to be set up and user name and passwords changed ... and if a newcomer misses any of the vital steps out it won't work.

Advise on sorting this out? I'm going to finish this article by reproducing a couple of paragraphs just written to a gent who was reporting zero length blobs in his database ...

"""The two most likely problems are that you don't have write permission set to allow for the web server to write the temporary disc files created, or that the directory in which they are to be written does not even exist. Judging by the fact that you are quoting the name of my blob field, I think you've pretty much copied the code from my site (and that's OK - that's what it's there for) but that makes it slightly less likely that it's too much of a coding error. Of course, without seeing your code / configuration details (I don't even know you're hosting server's operating system!) I can only guess in the dark!"""

"""Take it step by step - look at the temporary files to see that they're uploaded, look at the variable that hold the string to be put into the database and check that's non-zero along the way - then you'll have the process split into three pieces and will be able to find which one is the first one that's failing"""