Echoing through PHP an HTML image path which is dynamic - php

I am trying to echo a dynamic image path in PHP. I have this code that works
<?php
//this code works
$image = '<img src="img/newlogo.jpg">';
echo($image);
?>
//gives me an image
And this code that doesn't
<?php
//this code doesn't
$lineofstring='newlogo.jpg';
$image = '<img src="img/$lineofstring">';
echo($image);
?>
$lineofsting is actually going to be an image path which is stored in a mysql database where one row is filled with, for example: pictureabc.jpg, second row is picturexyz.jpg etc.
I am trying to pass on the searched imagepath name onto $lineofstring, which is then echo'd but no picture comes out. Where am i going wrong?

To interpolate variables in PHP, you need to use double quote marks ", e.g.
$image = "<img src=\"img/$lineofstring\">";
In this case, you need to escape the inner ".
You can also concatenate strings with .:
$image = '<img src="img/' . $lineofstring . '">';
if it's easier.

Your $lineofstring variable is in a string using single quotes. Single quotes tell PHP to use the string literally so your variable is not being recognized as a variable. Change it to double quotes or use concatenation to accomplish your goals.
$image = "<img src=\"img/$lineofstring\">";
Or:
$image = '"<img src="img'.$lineofstring.'">';

$profilepicture="images/profiles/".$myid.".gif";
$noprofilepicture="images/profiles/no_avatar.gif";
echo "<IMG SRC='";
if (file_exists($profilepicture)) {
echo "".$profilepicture."";
} else {
echo "".$noprofilepicture."";
}
echo "'>";

Related

Using php to echo a dynamic URL correctly

I am using php and conditional code to give a dynamic url to a photo. The result should read as http://example.com/biophotos/1.jpg. But instead I am getting
http://example.com/%22http://example.com/biophotos/1.jpg%22
How can I force it to just give the one url and without the %22 space on the end?
if ($emresult[0]['photo'] = "y") {
echo '<img class=\"alignright\" src=\"http://example.com/biophotos/' .
$theID . '.jpg" width=\"150\" height=\"150\">';
}
else {
echo 'There is no author photo.';
}
There is no reason in escaping double quotes when your using single quotes.
echo '<img class="alignright" src="http://example.com/biophotos/' . $theID . '.jpg" width="150" height="150" />';
As much as I agree with #slik, you also might want to look into %22 (double quotes) added to url out of nowhere
Check if magic quotes is on in your php.ini file. You can look into html_entity_encode() to encode the %22 as a slash.

How to correctly write <img src> in php without escaping to HTML

I am having trouble with my PHP code. I've been changing everything for 6 hours and I still get Parse errors no matter what I do. This is the code:
$slider3 = '<img src="'templates/' . $this->template . '/images/slider/slider3.jpg'">' . '" alt="' . $sitename . '" />';
The only way I can figure to not get it to throw an error is by writing it this way:
$slider3 = '<img src="templates/" . $this->template . "/images/slider/slider3.jpg" . "/>"';
but I don't think that's right.
I want $slider3 = "templates/MYTEMPLATE/images/slider/slider3.jpg" then later I will echo $slider3;
I get so confused with all the single and double quotation marks. I think the first one is right - I look at it and study it and it looks right to me. But it throws a parse error.
$slider3 = '<img src="templates/'.$this->template.'/images/slider/slider3.jpg"/>';
should work.
Explanation:
'<img src="templates/'
is a single-quoted string, which happens to contain a double-quote (which is needed for the html src attribute, or any other html attribute value really)
.
(dot) is the string concatenation operator. It concatenates ("glues") the first string together with...
$this->template
which is presumably a string containing the name of the template (not clear from your code example). Note that if $this->template comes from user input, or an otherwise unvalidated source, it could be used for cross-site scripting, eg. if it contains "><script>alert("XSS!")<script>, javascript is executed in the browser!
.
another concatenation with...
'/images/slider/slider3.jpg"/>'
which is another single-quoted string which happens to contain a double-quote, ending the src attribute value.
Try this:
$slider3 = '<img src="templates/"' . $this->template . '"/images/slider/slider3.jpg"/>';
$template = "MYTEMPLATE";
$slider3 = '<img src="templates/'.$template.'/images/slider/slider3.jpg"/>';
echo $slider3;
Will echo - >
<img src="templates/MYTEMPLATE/images/slider/slider3.jpg"/>
Just write:
<?php
$templates = "var";
echo "<img src='templates/${templates}/images/slider/slider3.jpg'/>";
it will result in
<img src='templates/var/images/slider/slider3.jpg'/>

Single and Double quote issues in print php variables

Am echoing php variables which works fine but when i tried to output image, nothing seems to work
working.php
echo ("addMarker($lat, $lon,'<b>$name</b>$address<br><br>$desc');\n");
not_working.php
for image display, i added
<img src='http://localhost/services/status/" .$pic. "'>
hence
echo ("addMarker($lat, $lon,<img src='http://localhost/services/status/" .$pic. "'>,'<b>$name</b>$pic<br><br>$desc');\n");
Any Help
The php documentation about strings should clarify your issue, i hope. In simple words, variables are not expanded (parsed) in single quotes.
Best solution is to use sprintf:
sprintf('<img src="http://localhost/services/status/%s">', $pic);
OK solution:
echo '<img src="http://localhost/services/status/' . $pic . '">'
Not so ok solution:
echo "<img src=\"http://localhost/services/status/$pic\">"

displaying uploaded images

I am trying to create a code which obtains uploaded images, stores them and also displays the image preview and confirms that the image was successfully uploaded.
<?php
$name=$_FILES['myfile']['name'];
$tmp=$_FILES['myfile']['tmp_name'];
$error=$_FILES['myfile']['error'];
$path='myweb/';
if(move_uploaded_file($tmp,$path.$name)==1){echo 'success';}else{echo $error;};
echo ('<img src="$path.$name" height="100px" width="100px"/>');
<?php
The problem is that images are not displaying.
I have also tried
echo ('<img src="$path$name" height="100px" width="100px"/>');
but it still doesn't work.
How can I get the images to display?
problem was with single and double quotes.
echo '<img src="' . $path.$name . '" height="100px" width="100px"/>';
You have used single quote and because of that it was not taking the variable name.
You have to include the variable in the printed string like this:
echo ('<img src="'.$path.$name.'" height="100px" width="100px"/>');
You can read more about it in the documentation.
If you don't do so, PHP will think that you want to print the text $path.$name instead the variables content.
<img src="<?php echo $path,$name; ?>" height="100px" width="100px"/>
OR
echo '<img src="' . $path.$name . '" height="100px" width="100px"/>';
There is a difference between using double "" and single '' quotes.
Double quotes are getting parsed, which means that
$variable = 10;
echo "$variable";
will output:
10
Single quotes don't get parsed:
$variable = 10;
echo '$variable';
will output:
$variable
you use variables within '' which means they don't get parsed.
i replaced line 13 in above code like so.
echo (" <img src=$path.$name height=100px width=100px/>");
And its working now.
Seems like double quotes were the problem here.
I'l read m0re about it.
Thank u s0 much guys u all've been very helpful.:)

render php variable comming from database

what I want to do is want to save php variable in database (suppose {$baseUrl} ) and I am getting the data on php page with echo command and on the same page I have defined $baseUrl='/public'. I want to get the value for the base url but I'm getting simply {$baseUrl} not '/public'
in db I have <img src="{$baseUrl}/img.jpg" />
on page I have
$baseUrl = "/public";
echo $content
it is giving <img src="{$baseUrl}/img.jpg /">
how can I get <img src="/public/img.jpg" />
Of course. Strings are strings, not PHP code. You'll have to replace it yourself, e.g.:
<?php
$baseUrl = '/public';
$string = '<img src="{$baseUrl}/img.jpg" />';
$replacements = array(
'{$baseUrl}' => $baseUrl,
);
echo strtr($string, $replacements);
I think what you want to do is a string replace. The variable pointer ($baseUrl) is a string that comes from the database. If you echo it, it is still just a string. What you need to do is something like this:
<?php
echo str_replace('$baseUrl', $baseUrl, $varFromDB);
?>
Or do I understand your question wrong?
With
str_replace($content, '{$baseUrl}', $baseUrl)
.
I am guessing that you are using the wrong quotes:
echo '<img src="{$baseURL}/img.jpg" />';
rather than
echo "<img src='{$baseURL}/img.jpg'/>";
You want the $baseUrl variable to be evaluated? You have to put the variable outside of the string definition:
echo '<img src="' . $baseUrl . '/img.jpg" />';
or put it between double quotes (strings enclosed in double quotes are parsed by PHP):
echo "<img src=\"{$baseUrl}/img.jpg\" />";
Based on your comments, you need this solution:
$content = str_replace("{$baseUrl}", $baseUrl, $content);
You may want to use the eval() function...
This is not a good idea, but it would achieve what you want.

Categories