Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Php-mySQL showing an image in a web page

Status
Not open for further replies.

maccherons

Programmer
Jun 3, 2004
9
0
0
IT
I have problems in visualizing images teken directly for my mySQL db.
I put the image in mySQL in a BLOB field, like I show in the following lines
where $bin_file is the binary I get from a form:

$data = addslashes(fread(fopen($bin_file, "rb"), filesize($bin_file)));
//esegui la query
$query = "INSERT INTO imgs (img) ".
"VALUES ('$data')";


and then retreive it with a normal query, say:

$query = "SELECT img FROM imgs WHERE id_img = $i_id";

To visualize it in a web page I have a php script where the core lines are this:

header('Content-Type: '.$img_mime);
header('Content-Disposition: "attachment"; filename='.$file_name);
print($img);

Now, the problem is that if you open that script with IE under winXP, it pops up to you
the choice open/save. Well, if you choose open, windows tells something like "no preview
available"; if you save it an then try to open the image you just can see it.

What's the problem? I can't work out this, I think it can be something in the way it passes
the stream, or the way IE bufferizes

Please, help
 
It depends from the type of image, it's dynamic. But it's right, because in the open/save dialog IE recognizes the type of file.
 
Well, that may not be a valid assumption. IE tends to pay a great deal more attention to file extensions than to "Content-Type" headers.





Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Yes, maybe I'm trusting to much in it's potential.
However the mime type for a jpeg was "image/jpeg
 
Nothing to do neither writing manually the MIME type or the filename. I was hopeful. Maybe IE doesn't bufferize the file. I really don't know what to do.
Other ideas?
I know you can do it
 
What do you mean? If you know another way to do it it's ok. It works with other types than images (I'm using this for technical sheets of products), and it works with images under mozilla.
The damn combination is IE-XP image viewer, but I think the most of the users use this combination
 
I mean, what are you trying to do with these images?

Are you trying to display them on a webpage or provide them for download?

If it's the former, you should stop sending the "Content-Disposition" header. If it's the former, don't expect to be able to view the image until you've saved it to your local system's filesystem.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Depending on the size of your project, you may quite fast fill up the available DB space?!

In the past, I have done this:

1. Upload the image to a "temporary folder" on the server.

2. Add a record to your DB to a table with a unique ID #

3. Fetch this ID #

4. Rename the file to something like 0000ID# remember to maintain the original file extension.

5. Move the file to the folder you wanna store images in.

... etc. You get the idea.

Advanteges?

-requires only a few bytes DB space per image file.
-very fast DB transactions.

Good Luck


Jakob
 
Thank you all.
At last I changed my mind and put those images on a new window popping up from a link. First idea was to provide images for download, but in fact I did'n achieve to let the users open them without saving (and you tell me it's impossible. I thought I was doing something wrong).
I even thought to the idea of storing images into files with a hook in names, but then I've chosen the db way to try it (I'm a programmer but also a student) and to keep it easy for backups. Obviously the more convenient way is the files one, but it's too late now: customer wants his application. Maybe I will change it later.
Is db space so much limited (I use mySQL)?

 
Hi maccherons,

I use MySQL too on my two domains. The storage limitation seems to follow the cost of the hosting.

My first domain has a 100 MB limit -all included (web space, mails, db). I pay 16 $ per month for that one.

My second has a 125 MB limit -all included (web space, mails, db). I pay 20 $ per year for that one....

Guess which supplier I'm skipping soon §;O)

Good Luck with your project!


Jakob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top