Forum Moderators: coopster

Message Too Old, No Replies

Dynamic Image Display Based on PHP Variable

         

plasmicsteve

4:18 pm on Dec 14, 2009 (gmt 0)

10+ Year Member



Hello. I have what I hope is a simple PHP question.

Say I have a directory with a bunch of images named "123456.jpg", "123457.jpg", etc.

And I have a link constructor that creates links with the image name as a variable (without the ".jpg") - like this:

http://www.example.com/imagedisplay.php?image=123456

I want to create a page that parses that variable, then places the image on a page - pulling in the image source from a location like this:

http://www.example.com/images/image123456.jpg

How can I do that?

Thanks in advance.

[edited by: dreamcatcher at 12:38 am (utc) on Dec. 15, 2009]
[edit reason] use example.com, thanks. [/edit]

rocknbil

8:10 pm on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$img_dir = '/images';
$img_prefix = 'image';
$ext = 'jpg';
//Avoid attempted hacks with a little filtering.
if (preg_match('/^\d+$/',$_GET['image'])) {
// or use is_numeric()
$src = '<img src="' . $img_dir . '/' .
$img_prefix . $_GET[image'] . '.' . $ext .
'">';
echo $src;
}
else { die("Hmm, not numeric.") }

plasmicsteve

8:25 pm on Dec 14, 2009 (gmt 0)

10+ Year Member



Wow - great, rocknbil - I'll try it out and I'll let you know the results. I appreciate it - especially the anti-hacking bit that I'd never have figured out on my own.