Image Switch

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Image Switch

Post by tecktalkcm0391 »

Is their a way to make a image switch... Like the image url would look like: images.php?4545 and then the php code would go and get the image that matches up with the code 4545 and send it as a image file back to the page that is requesting the image?
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

A simple version...

image.php?id=1234

Code: Select all

<?php
//query database for file location and mime type
header("Content-type: " . $mime_type);
readfile($file_location);
?>
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

You want to force a download of an image? Then you'll want to user header(); Something like this should work:

Code: Select all

 
if(is_file($CFG->absolute_path.$filepath)){
    header("Cache-control: private");
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=".$file);
    header("Content-length: ".filesize($filepath)."\n");
    //send file contents
    $fp=fopen($filepath, "r");
    fpassthru($fp);
} else {
	header('location: http://'.$CFG->domain);
	die();
}
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 »

Do a search for 'secure download' or 'PHP as image' or something similar. This has been discussed many times before.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Why not just stick to a proper image URL? What's your point?
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

I wanted the php code to get a file based on inputed data VIA Get from the URL.

Like for an avator type thing:
page.php?gender=male&picture=3

The page.php file would then be like ok i have to get male 3 and then get the picture: image_male_3.gif

If that would work, then my website creation would be a lot easier.
Post Reply