473,394 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

images with php

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
7 1184
pbmods
5,821 Expert 4TB
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
3,237 Expert 2GB
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
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
5,821 Expert 4TB
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
5,058 Expert 4TB
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
$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
1,726 Expert 1GB
[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

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

Similar topics

7
by: Wayne | last post by:
I have a script that uses filesystemobject that reads files from a given path, in my case images. It is running on a server that is 2000 adv svr w/ all current patches. The script prior to some...
3
by: Simon | last post by:
This problem has been driving me mad for months.... Seen a few posts on forums about it but no answers... No mention on MSDN etc. XP Pro SP1, VS.NET (c#) .Net framework 1.1, IIS 5.1. In a...
10
by: Neo Geshel | last post by:
I am seeking to hand-roll my own blog in ASP.NET 2.0 and SQLExpress 2005. Why? Because I can. Because I will gain experience. The one thing that has me stumped at square one is inline images....
6
by: wattanabi | last post by:
Greetings, I'm attempting to layout a bunch of images in a grid using DIV's instead of a table. I currently have a 3x6 table that I need to convert to css. I've seen various example of a 3 to 4...
2
by: mouseit101 | last post by:
Hi, I'm writing a script that would (hopefully) search google images for whatever, and then return a list of URLs for the image. Right now I have: $dom = new DomDocument(); $url =...
0
by: Frenchie | last post by:
Hi, I have created a very neet menu from an example found on the MSDN library at: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.menuitembinding.imageurlfield.aspx My...
1
by: Cerebral Believer | last post by:
Hi folks, I am using the following code for mouse over (roll-overs) in my XHTML code. <a onmouseover="document.getElementById('sitemap').src = '../images/buttons/sitemap_mo.jpg';"...
4
toxicpaint
by: toxicpaint | last post by:
Hi, can anyone give me a hand. I'm currently displaying 4 random images at the top of a page. I did this using an array of 35 pictures and then writing them to page. The problem I have is that in...
5
by: remon87 | last post by:
I need some help. I have javasript that creates the submenu but it works if I have a text with css. I need it to do the same with a roll over images. so when I click on the image the submenu...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.