Link to home
Start Free TrialLog in
Avatar of Adam
AdamFlag for United Kingdom of Great Britain and Northern Ireland

asked on

How can I enter images into a MySQL database correctly through my MyPhPadmin?

I have set up a dynamic database using Macromedia Dreamweaver, MySQL and MYPHPadmin and have managed to connect to my database through Dreamweaver, and am able to see the data when I preview my webpage. All's well there.

I now want to add images into my MySQL Database. I have tried to do this by entering another column in my table (through MYpHpadmin) and setting it to type 'LONGBLOB'. Attributes were then set to BINARY - (automatically, I think).

I then edited the field as per usual, and browsed (still in MyPHPAdmin) to my image on my local computer. (My MySQL and MyPhpAdmin are stored on a remote server).I seemingly selected an image and pressed 'GO'. The image seemed to go into the database as the filesize was then dispalyed in the appropriate column for the appropriate field in the table (although I could see no other evidence that it was actually in there).

Through Dreamwaver, I then linked a placed image to this column on the database (selecting it through a set-up recordset) but when I tried to view this in my browser, instead of my nice little image, I saw lines and lines of nonsense (non-decipherable letters and symbols).

Have I entered the images into the Database correctly? Any help / opinions on the matter would be greatly appreciated.

Thanks,

Adam

Avatar of Silversoft
Silversoft

Hi

There are actually three popular ways of uploading images in PHP

1.) Hardest method. save the image in the database as type BLOB. Load the image file either from the upload, as normal, or from another disc location. Read the file into a variable using file_get_contents() or similar. You need to pay careful attention to the type of the file, either using $_FILE[whatever][type] which is e.g. "image/gif".

Now, when you output the file, send a header("Content-type: image/gif"); and then output the contents of the BLOB. It should work fine.

2.) Easier method. Just upload the file, save it to disk, but store the filename in the database.

3.) Easy, but not easiest method. Upload the file, save it to disk using a unique identifier as filename (i.e. the auto_increment column from mysql), then save the original filename in the mysql database.

http://www.webmasterworld.com/forum88/4975.htm

I think you are doing the hardest way (1). I would recommend you to do option 3, as it is easier and more eiffient. I think that how most people do it, including myself.

regards-
Avatar of Adam

ASKER


Hi Silversoft,

Thanks for the advice. Unfortunately, I couldn't view the thread you sent as I was unable to log on to that site, and the service required a subscription payment to get log-on details (not too cheap either).

Sorry if I am asking what may seem obvious, but with option three do I save the file to a location on the remote server (where I assume the database also is)? Anywhere in particular? Then, should I save the image using a unique filename and then save that filename in the mysql database? Would I save it as type varchar or binary or...?
Also, how does the mysql database associate the image with just a filename. Sorry for all these questions....

Thanks,

Very much appreciated

Adam

ASKER CERTIFIED SOLUTION
Avatar of Silversoft
Silversoft

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adam

ASKER

Okay. Thanks for that. I'll try to make it work now.

Thanks.

Adam
no prob :)

Good luck...
You should base64encode whatever you put into your BLOB, so you'll maintain the integrity of the file. That what i use for my MySQL image admin script and it works great.