0

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"

Edited by Misklahr: n/a

3
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by Misklahr
0

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.

0

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.

0

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])

0

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');
0

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

Thank you very much!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.