Link to home
Start Free TrialLog in
Avatar of gvijay1
gvijay1

asked on

Uploading an image into a table in MQSql using PHP

Does anyone have PHP code that can be used to upload an image into a specific table in MySql.

Avatar of elran70
elran70

Let's say you have a table described like this:

CREATE TABLE T_MyTable (
F_Code INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
F_Blob LONGBLOB
);

by uploading the blob content from file (image, binary, whatever ...), using:
$DATA = addslashes(fread(fopen($FILE, "r"), filesize($FILE)));

And then insert it using this SQL satatment:
INSERT INTO F_MyTable (F_Blob)
VALUES ('$DATA');

This example is basic, you can use a ready made file on server, or just loaded using POST file load.
To display an image in this case, you should use the Header file to set the type of the content, and then dump the data you have read from the database using a simple query.
Please be a ware!!! that you have size limits regarding timeout of php script exececution, packet size in mysql server, upload limit on web server
Eldad.


Avatar of gvijay1

ASKER

Ok, I might need a litte bit more clarification.

Assuming I have an HTML form where the user is going to entering the file to upload. I need to know the following:

1. How do I browse the user's local directory for the image that needs to be uploaded.
2. Once the image has been located, I am assuming I am going to submit this to a php script.
3. What would I need in the php script to extract the name of the image and then upload it to a particular directory (since I cannot store an image directly in a DB right? I would need to store it in a directory and it's name in a table. I would then use the name from the table as a link to display the image).

Please let me know if this makes sense.
Avatar of gvijay1

ASKER

Follow up question...

I noticed you use the header to tell the browser that it is an image. Lets say that I extract the image content from the table, and want to display it on the HTML page, how would I set this header. please give me an example.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of elran70
elran70

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 gvijay1

ASKER

Thanks for the detailed explanation.