MySQL - Storing images?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Exdaix
Forum Newbie
Posts: 5
Joined: Fri Feb 07, 2003 4:18 pm
Location: Near Philadelphia, PA, USA
Contact:

MySQL - Storing images?

Post by Exdaix »

How would I use a form to upload images to a MySQL table?
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

First of all, make sure you have a field in your table defined as a BLOB
(Binary Large OBject)

You form tag will look like this:
<form method="post" name="frmUpload" action="fileupload.php" ENCTYPE="multipart/form-data">

The form element where they select the file to upload:
<input type="file" name="txtTheFile"> - that generates a "Browse" button for them to choose the file from their local file system.

After that it is little more than inserting the file via Sql.

Phil J.
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

images

Post by phpScott »

Unless you have real a really solid reason for wanting to store images in a db I generally find it to much of a hassel, and takes up to much space unless there are real restrictions on the size of the images to be stored.

My appoaroch is to just store the name of the image in the db and then use that to to create the src in the <img > tag.

you still need to do the same thing as fractalvibes said about the form and input tags. But you will need to copy the file to your images directory as it just gets copied to the tmp directory on the server.

phpScott
If you want a sample script on how to do this just post and I will send you a copy of mine.
Exdaix
Forum Newbie
Posts: 5
Joined: Fri Feb 07, 2003 4:18 pm
Location: Near Philadelphia, PA, USA
Contact:

Post by Exdaix »

That sounds great phpScott, so what you do is upload the images to a directory with the POST form, and copy thier names to a table in the database? I think I am understanding it alright.

Could you post that code?

Thank you for your time.
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

Yes, much as phpScott said - most of the time you are better off just storing the link to the image rather than the image itself. There do arise occasions where storing in the database makes sense - Apps on two different servers that needed to pull from a common set of images, neither could see each other's file systems, a question of replication or using a common database they could each talk to...

Phil J.
Exdaix
Forum Newbie
Posts: 5
Joined: Fri Feb 07, 2003 4:18 pm
Location: Near Philadelphia, PA, USA
Contact:

Post by Exdaix »

phpScott... you there?
Post Reply