Hi

I have a website that has a get.php file that echoes out the specific image i want

Here is the code:

$id = addslashes($_REQUEST['id']);

$image = mysql_query("SELECT * FROM items WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-type: image/jpeg");


echo $image;

To get an image from the database with the id $id, i use this code:

img src='get.php?id=$id'

The problem is that when someone wants to save the image, the URL is for example "mywebsite.com/get.php?id=137" and the webbrowser wants to save it as "get.php".

I want it to be saved as just "$id", or as in this example "137"

I'm no expert but I think you need some error checking to debug your code

here is a suggestion of 1 thing that could be wrong

"$image = mysql_query("SELECT * FROM items WHERE id=$id");"

I think should be

$image = mysql_query("SELECT * FROM items WHERE id='$id'");

Quotes around the variable.

I could be wrong, as I haven worked with images, but all my Query strings are like that.

You need to use a .htaccess file (assuming you're using apache) and setup rewrite rules.

RewriteEngine On
RewriteRule ^get/([0-9]+)$ get.php?id=$1 [NC,L]

Something like that will probably get you what you want.

My .htcacces file looks like this:

ErrorDocument 404 http://www.mywebsite.com/404.html

RewriteEngine On
RewriteRule ^get/([0-9]+)$ get.php?id=$1 [NC,L]
RewriteRule ^([a-z0-9\-]+)$ /$1.php [L]

(The last "RewriteRule" deletes the .php at the end)

But the one mschroeder wrote didn't work x/
(RewriteRule ^get/([0-9]+)$ get.php?id=$1 [NC,L])

My rule was actually tested and does work.

RewriteEngine On
RewriteRule ^get/([0-9]+)$ get.php?id=$1 [NC,L]

The NC makes it case insensitive and the L makes it stop processing once it has matched on that rule.

The second rule you posted in there will never be reached in the schema domain.com/get/123 as the first rule satisfies the match.

Perhaps your mod_rewrite module is not enabled?

If you're doing this simply to change the name of the file that is being saved it would be much more effective to send the correct headers with the file.

header('Pragma: public');
header('Cache-control: must-revalidate, post-check=0, pre-check=0');
header('Cache-control: private');
header('Expires: 0');
header('Content-Type: image/jpeg');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize('filename.jpg') );
header('Content-Disposition: attachment; filename=filename.jpg');

Oh, sorry, nevermind, I now understand what you mean, mschroeder.

Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.