How to get images from a database and display the image?

Need help with Photoshop, the GIMP, Illustrator, or others? Want to show off your work? Looking for advice on your newest Flash stuff?

Moderator: General Moderators

Post Reply
User avatar
ravervx
Forum Newbie
Posts: 7
Joined: Fri Sep 24, 2004 1:26 am

How to get images from a database and display the image?

Post by ravervx »

I'm writing a web gallery. All the pictures are saved in a database. I can retrive the images fine, by using <img src="diaplay.php"> to dislay images. Where the diaplay script would get the binary data from the database and convert it into a jpeg and return it to the browser.
But this way, when the user try to save the image, it gets display.php as the file to save. I have the original picture filenames saved in the database. How can I return the image files with thier original file names? By using a javascript or something of sort?

Please help.

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

read the [php_man]header[/php_man] function page, or search here about [devnet]+force +download[/devnet]
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Ya, what ~feyd said. In other news - I wouldn't suggest storing the images in the DB if this gallery is going to be used too much. Storing images in the DB is cool and does simplify some things, but in my experience, page load times do increase quite a bit.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ravervx
Forum Newbie
Posts: 7
Joined: Fri Sep 24, 2004 1:26 am

Post by ravervx »

thanks for the answer
Actually, right now I'm storing the pictures in the file system. But I renamed them with a randomly generated name for security.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I think the best solution would be to store the images in a directory outside the document root. Then, the images can't be viewed by simply typing in their URL. You can still use a display.php type file, but that would dump the data from the filesystem, rather than the DB.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

for more info on the images and databases type stuff: [devnet]+image +database -pag*[/devnet]
User avatar
ravervx
Forum Newbie
Posts: 7
Joined: Fri Sep 24, 2004 1:26 am

Post by ravervx »

Thanks feyd and pickle, you guys are a great help
User avatar
ravervx
Forum Newbie
Posts: 7
Joined: Fri Sep 24, 2004 1:26 am

Post by ravervx »

One more question,

I can't seemed to use require_once() to include files in an upper level directory

For example, config.php is within the folder /
and the file using config.php is within /folder/filetouseconfig.php

I used syntax
require_once(../config.php);

But that does not work, I checked the manual for the function, but nothing is mentioned regarding that.

Thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I don't know if this is necessary, but I always put my filepath in quotes.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ravervx
Forum Newbie
Posts: 7
Joined: Fri Sep 24, 2004 1:26 am

Post by ravervx »

Sorry, when I wrote the example I forgot the quotes around the filename.
It suppose to be require_once("config.php");
But I guess the orignal question still stands

Thanks
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Requiring (sp?) is a fatal error, so if it's not working, what error does it display? If it doesn't display an error, then the requiring is working and you may need to check the file you're requiring.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your include_path either needs adjusting or you aren't including/requiring in the right path.. check the output of your phpinfo(), or use $_SERVER['DOCUMENT_ROOT'] to get the web root of your site.
User avatar
ravervx
Forum Newbie
Posts: 7
Joined: Fri Sep 24, 2004 1:26 am

Post by ravervx »

Hi,

Since the last time I posted the original question, I have written up my php script. But recently I was doing testings in IE 6.0 and though the image loads, when I right click on it to save, it saves as untitiled type bmp.

Code: Select all

header("Content-type: &#123;$type&#1111;'mime']&#125;");
header("Content-disposition: filename=$filename");
header("Content-length: " . filesize($image));
return readfile($image);
Is there any fix for it? Or there is something I'm missing?

Thanks
Post Reply