By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

images with php

P: 30
how do i put an image in my php script. i want to be able to do it so that the whatever choice they make a picture comes up , i can do if else if loops for everything but cant put an image in could someone give me an outline on adding images

thanx
May 8 '07 #1
Share this Question
Share on Google+
7 Replies


pbmods
Expert 5K+
P: 5,821
how do i put an image in my php script.
Alrightey.

Here's the fast way to do it:

[PHP]
<?php
header('Content-type: image/jpeg');
readfile($_REQUEST['imageNameOrWhatever'] . '.jpg');
exit;
?>
[/PHP]

Nothing should be before the opening <?php tag, or else your output won't look right.

Note that the request variable doesn't contain the full name of the file; this would prevent a resourceful cracker (or clueless User) from loading up, say, ssh_root_password.txt ("I'd been meaning to get rid of that!").

As for the good way to do it, well, you'll probably want to validate your $_REQUEST['imageNameOrWhatever'] variable, and just to be safe, you should probably prepend the path, so that the User only has control over the name of the image. E.g.,

[PHP]
readfile("$_SERVER[DOCUMENT_ROOT]/images/local/stuff/and/whatnot/$_REQUEST[imageNameOrWhatever].jpg");
[/PHP]

Beyond that, go nuts; just make sure that the contents of the image file is the ONLY thing that gets output.
May 8 '07 #2

Motoma
Expert 2.5K+
P: 3,235
Are you trying to generate an image using PHP (or read one dynamically), or just embed an image into the HTML?

how do i put an image in my php script. i want to be able to do it so that the whatever choice they make a picture comes up , i can do if else if loops for everything but cant put an image in could someone give me an outline on adding images

thanx
May 9 '07 #3

P: 30
i have a if loop where if the users chooses an option a price comes up i want images which i already have to come up depending on the option
May 9 '07 #4

pbmods
Expert 5K+
P: 5,821
i have a if loop where if the users chooses an option a price comes up i want images which i already have to come up depending on the option
In that case, your best bet would likely be just to generate a bunch of img elements.

[PHP]
$image = (isset($_GET['imageName'])
? $_GET['imageName']
: 'default'
);

print("<img src=\"images/$image.jpg\" alt=\"$image\" />");
[/PHP]

Where $_GET['imageName'] is set by reloading the page when the User chooses an option. Or something like that.

Incidentally, to address something I said in a previous post, when you use a PHP script to output an image (like in my first post), you should only need to use that when the image is OUTSIDE of your server's document root (such as stored in a database or a temp directory, for example). Otherwise, that's just way overkill when a simple img tag will do very nicely.
May 9 '07 #5

Atli
Expert 5K+
P: 5,058
i have a if loop where if the users chooses an option a price comes up i want images which i already have to come up depending on the option
I dont really understand your problem. Could you post your code?
May 10 '07 #6

P: 30
$extras = $_GET ['extras'];


if ($extras == "mints")
{
$extracost = 5;
}

elseif ($extras == "shovel")
{
$extracost = 8;
}


elseif ($extras == "straw")
{
$extracost = 20;
}


elseif ($extras == "camel food")
{
$extracost = 10;
}


elseif ($extras == "leather saddle")
{
$extracost = 15;
}


elseif ($extras == "brush")
{
$extracost = 10;
}


elseif ($extras = "file")
{
$extracost = 8;
}

else
{
$extracost = 0;
}


echo "
<p>The extra you choose was an $extras at the cost of $extracost</p>";


thats part of my code now i want an image to come up when they pick the extra option i already have the images
May 14 '07 #7

code green
Expert 100+
P: 1,726
[PHP]elseif ($extras == "brush")
{
$extracost = 10;
$img = 'www.address/of/your/image.gif';
$alt = 'description of image';
}


elseif ($extras = "file")
{
$extracost = 8;
$img = 'www.address/of/your/image.gif';
$alt = 'description of image';
}

else
{
$extracost = 0;
$img = 'www.address/of/your/image.gif';
$alt = 'description of image';
}
echo "
<p>The extra you choose was an $extras at the cost of $extracost</p>
<img src=\"$img\" height=\"size\" width=\"size\" alt=\"$alt\";[/PHP]
May 14 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.