Uploading images/other binaries to MySQL via PHP
Posted by admin (Graham Ellis), 10 September 2007An excellent question here (via email) from a correspondent who needs to upload pictures for his shop site and found my example on our web site
The question is not a new one ... so I thought it worthwhile pulling all my resources into one place and answering fully ...
The script you have found puts together many different aspects of http, PHP and MySQL - although it is short, it is not simple and it is not intended as a script for newcomers. You'll see a lot of comments submitted by people saying how great it is, but a few saying that they cannot get it to work and that's because there are server settings to make, such as directories, passwords, etc - and probably URLs to change too.
I won't know exactly which page you found, but the complete example comprises the following pages
Article on subject
Looking at uploaded images
Sourec code - viewing
source code - upload
Have a look through all of these pages - the first link especially which has tips and instructions at the bottom of it and which answer most of the common questions. Take it step bt step - see if you can get the form up with the same tags in the HTML as mine, then if you can get the temporary file onto the server, then into the database. Finally test if you can get it back. Splitting your problem down will help you find where it is.
If the description above leaves you asking "how do I do THAT?" then you're probably not yet at the stage to be comfortably working with the script and should learn some more basic stuff. I would normally recommend our course (running next week!) at http://www.wellho.net/course/phfull.html or thath you drop by one evening, but I suspect that you're not from the same part of the world that we're in; you should, then, be able to find someting along the same lines in your area.
I'll finish we another, simpler, example that uploads .pdf files ... you may get some additional clues from these pages:
Upload form - source
Store script - source
View script - source
Posted by keith (keith), 8 November 2008Suggestion...don't put files into MySQL as this will bloat the database..just put the link/url in mysql.
If you want to protect the images, put them outside the document root and allow php to grab them.
Posted by admin (Graham Ellis), 8 November 2008on 11/08/08 at 04:32:25, keith wrote:
There are arguments both ways, Keith ... there's going to be bloat somewhere and I prefer to bloat my databases (which are designed for that sort of thing) rather than to bloat directories with thousands and thousands of individual files. I also prefer to have them in my database as it enforces a unique accessor, rather than risk people finding / changing / deleting files directly on the file system and not appreciating the database connection. Think of someone deciding that a file name is mis-spelled and renaming it ... not realising that the database refers to it by name. Result - one broken database!
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: firstname.lastname@example.org • WEB: http://www.wellho.net • SKYPE: wellho