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

AdamTrying to learn phpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SilversoftCommented:
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-
0
AdamTrying to learn phpAuthor Commented:

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

0
SilversoftCommented:
Log on to that thread?? strange, I can get into that thread, but anyway, it just a source where i got the 3 options from.

Alright let me walk ou through how option 3 is done:

1.) Set up your HTML form with something like this (enctype="multipart/form-data" is crucial, it must be there)

<form action="" method="post" enctype="multipart/form-data" name="form1">
  <input type="file" name="image">
  <input type="submit" name="Submit" value="Submit">
</form>

2.)In your upload image and save to database page, this is what you should do:

$query = mysql_query("INSERT INTO images VALUES ('','$_FILES[image][name]')"); //save the image filename into the databse

$image_id = mysql_insert_id(); //get the auto-increment id from the last record inserted into DB

copy ($_FILES['image']['name'], "my_upload_path/$image_id.jpg"); //save the uploaded file to it's destination

3.) To display the picture:

let [image_id] = 1 or 2 or 3 or whatever the unique id is

<img src = "my_upload_path/[image_id].jpg">

Hope you understand the logic of it....
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

AdamTrying to learn phpAuthor Commented:
Okay. Thanks for that. I'll try to make it work now.

Thanks.

Adam
0
SilversoftCommented:
no prob :)

Good luck...
0
sub_coolCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.