PHP file_get_contents with if/else - php

On this link I was learning about file_get_contents and I'd like to incorporate it into a simple if/else statement but can't seem to get it correct.
I'd like to check if the file exists and if not, display a no_image.jpg graphic.
My struggle is that the tag already has a PHP echo so it is causing the snippet to stop working. How do I format the if/else while still haveing the use the dynamic value from the echo if file_get_contents is true?
Currently I have a simple tag like this:
<img width="200" height="150" src="images/<?php echo(rawurlencode($row['MLS_NUMBER'])); ?>_1.jpg" alt="" align="left" vspace="3" hspace="3" />

Basically:
<?php
if (file_exists(...)) {
$path = ...;
} else {
$path = 'no_image.jpg';
}
?>
<img src="images/<?php echo $path; ?>" />
//add a semicolon after echo $path otherwise it won't work

Assuming you are looking for a jpg file based on $row['MLS_NUMBER']
<img width="200" height="150" src="images/
<?php
if (file_exists($row['MLS_NUMBER']) {
echo(rawurlencode($row['MLS_NUMBER'])).'_1.jpg';
} else {
echo "no_image.jpg";
}
?>" alt="" align="left" vspace="3" hspace="3" />

Related

Remove HTML code and output in PHP destination url

I wanted to pull out html code Target URL and forgive with a variable
<img src="https://www.test.de/adtrack/3a10056f3.img" border="0" />
$url = https://www.test.de/adtrack/3a10056f3.html
the $url wants to echo that too in php
Just echo the whole thing with your variable added in it.
$url = 'https://www.test.de/adtrack/3a10056f3.html'
echo '<img src="https://www.test.de/adtrack/3a10056f3.img" border="0" />';
or, echo it inside your html like this:
<img src="https://www.test.de/adtrack/3a10056f3.img" border="0" />
If you do as above, remember that your file extension should be ".php" and not ".html" or else it won't work.

Assign Image SRC in php

After retrieve image path from database I want to pass it to <img> tag to show up,
I read this solution, but I do not want to echo it, I want to assign it as src for image tag
I tried this:
$image = mysqli_fetch_array($user_images);
$img1 = $image['file_path'];
$imageData = base64_encode(file_get_contents($img1));
$src = 'data: '.mime_content_type($img1).';base64,'.$imageData;
How can I assign $src to image, I tried this but no luck :(
<img id="img1" width="150" height="150" src="' . $src . '"/>
Thanks in advance.
Inside the HTML you still need to echo the value of $src. This should work:
<img id="img1" width="150" height="150" src="<?php echo $src; ?>"/>
Try with PHP tags like below:-
<img id="img1" width="150" height="150" src="<?php echo $src;?>"/>
Reasonable solution I can think of is
<img id="img1" width="150" height="150" src="<?php echo $src; ?>"/>
but the code where you get the image source has to be above the image tag, so you have the $src.
You are using PHP language and in Php, you can't pass variable directly in the Tag you have to set a variable in the PHP environment.
To create Php environment you can right <?php ?> or you can use to echo variable <?= ?> between these PHP tags you can pas your variables to echo and assign or anything else what you want to do.
<img id="img1" width="150" height="150" src="<?= $src ?>"/>

Image Not Showing up in if statement

This should be simple. Trying to echo an image if it exists in a directory. Else guest.png should echo. So far, the guest.png is echoing fine. My problem is that I can never the .jpg image when it does exist. I have double-checked the source. The image just shows up as an empty image box.
<?php
$ID=$row_RecordsetLast['ID'];
$image = '../../pics/'.$ID.'.jpg';
if (file_exists($image)) {
echo '<img src=$image alt="" width="110" height="161" />';
} else {
echo '<img src="guest.png" alt="" width="110" height="161" />';
}
?>
You're not wraping the image address with apostrophes or quotes.
Change the line to this:
echo '<img src="$image" alt="" width="110" height="161" />';
Should work, considering the given address will be reached by the HTML page.
Have in mind that ../../pics/ may not work depending on your project's folder structure.
After trial and error, the following code worked for me. I guess variables did not work well within img source.
<?php
$ID=$row_RecordsetLast['ID'];
$image = '../../pics/'.$ID.'.jpg';
if (file_exists($image)) {
echo '<img src="../../pics/' . $ID . '.jpg" alt="" width="110" height="161" />';
} else {
echo '<img src="guest.png" alt="" width="110" height="161" />';
}
?>

why do two pages, 100% the same code, both including the same style.css have different output?

i had a strange situation where a function that prints the navigation section of the website, was giving slightly different output on one page than the other. first started when i was adding which i didnt have declared before. the function that prints the navi was given the same arguments on both pages. yet one page was outputting it with extra breaks or extra margins or something.
so i spent alot of time messing with the style.css but no matter what, the one page always had more space in between the links. changing the style effected both pages but the one page always had more spacing or margins or breaks or whatever it is.
then i made sure both pages had no xml validation errors, made it perfect in that sense, still the one page had more spaces between the links. WHEN THE EXACT SAME FUNCTION TO PRINT THE NAVI IS BEING CALLED...
finally I had had it, i copied all of page a's code (the page displaying properly) and i pasted it into page b's code (pasting over b's old code) and i saved and to my jaw dropping awe, page b is STILL SHOWING WITH EXTRA SPACES.
to be clear, 2 files, code in the files is 100% identical only the name of the documents are different. and yet the output on the one is still double spaced. And i repeat again, both files loading the same style.css and yet output is different...
someone PLEASE let me know how this is possible. and better yet how to fix it.
i dont really see how code example is necessary with all being said but i'll give you the function that prints the navi
function navigation ($str){
settotals(0);
if ($_SESSION['lang'] == "eng")
{
$home = "Home";
$login = "Login";
$insert = "Insert";
$post = "Post Request";
$ranks = "Ranks";
$score = "Scoreboard";
$register = "Register";
$howtoread = "Read Japanese";
$contact = "Contact Us";
$engreq = "English Requests";
$japreq = "Japanese Requests";
$japtra = "Japanese Translations";
$engtra = "English Translations";
$mytran = "My Translations";
$mylikes = "My Likes";
$likesrec = "Likes Received";
$pref = "Preferences";
}
else
{
$home = "ホーム";
$login = "ログイン";
$insert = "入力";
$post = "リクエストを掲示";
$ranks = "ランク";
$score = "スコアボード";
$register = "登録";
$howtoread = "日本語の読み方";
$contact = "コンタクト";
$engreq = "英語のリクエストリスト";
$japreq = "日本語のリクエストリスト";
$japtra = "英和訳リスト";
$engtra = "和英訳リスト";
$mytran = "自分の翻訳リスト";
$mylikes = "「いいね」を押したリスト";
$likesrec = "「いいね」をもらったリスト";
$pref = "選択肢";
}
?>
<img border="0" src="<? echo $str; ?>img/blankbutton.jpg" width="152" height="26"/> <p><font face="Arial" size="4">
<?echo $home?><br /><img border="0" src="<? echo $str; ?>img/line.gif" width="137" height="1"><br />
<?echo $login?><br /><img border="0" src="<? echo $str; ?>img/line.gif" width="137" height="1"><br />
<?if ($_SESSION['rights'] >= 4)
{?>
<a href="<? echo $str; ?>insert" > <?echo $insert?></a><br /><img border="0" src="<? echo $str; ?>img/line.gif" width="137" height="1"><br />
<? }?>
<a href="<? echo $str; ?>search" > <?echo $post?></a><br /><img border="0" src="<? echo $str; ?>img/line.gif" width="137" height="1"><br />
<?echo $ranks?><br /><img border="0" src="<? echo $str; ?>img/line.gif" width="137" height="1"><br />
<? echo $score?><br /><img border="0" src="<? echo $str; ?>img/line.gif" width="137" height="1"><br />
<?echo $register?><br /><img border="0" src="<? echo $str; ?>img/line.gif" width="137" height="1"><br />
<?echo $howtoread?><br /><img border="0" src="<? echo $str; ?>img/line.gif" width="137" height="1"><br />
<?echo $contact?><br />
</font>
<font face="Arial" size="2">
<br />
<img border="0" src="<? echo $str; ?>img/Browse.gif" width="152" height="26"><br /><br />
<img border="0" src="<? echo $str; ?>img/arrowleft.gif" width="11" height="11"/> <?echo $engtra?><br /><br />
<img border="0" src="<? echo $str; ?>img/arrowleft.gif" width="11" height="11"/> <?echo $japtra?><br /><br />
<img border="0" src="<? echo $str; ?>img/new.gif" width="152" height="26"><br />
<br />
<img border="0" src="<? echo $str; ?>img/arrowleft.gif" width="11" height="11"/>
<?echo $engreq?>
<br /><br />
<img border="0" src="<? echo $str; ?>img/arrowleft.gif" width="11" height="11"/>
<?echo $japreq?>
<br /><br />
<?if ($_SESSION['username'])
{ ?>
<img border="0" src="<? echo $str; ?>img/promo.gif" width="152" height="26"><br /><br />
<img border="0" src="<? echo $str; ?>img/arrowleft.gif" width="11" height="11"/> <?echo $mytran?>
<br /><br /> <img border="0" src="<? echo $str; ?>img/arrowleft.gif" width="11" height="11"/> <?echo $mylikes;?>
<br /><br /> <img border="0" src="<? echo $str; ?>img/arrowleft.gif" width="11" height="11"/> <?echo $likesrec;?>
<br /><br /> <img border="0" src="<? echo $str; ?>img/arrowleft.gif" width="11" height="11"/> <?echo $pref?>
<?
}
?></font></p>
<font face="Arial" size="2"><?
if ($_SESSION['username'])
signature($_SESSION['username'],"");?></font><br />
<?
}
1.) Calling the same function with the same arguments does not neccessarily produce the same output. There might be other factors that influence the output, like environment variables, software versions, and and and.
So not the fact that the function is called the same way is of interest, but only if the output is really identical. Did you check that? You don't mention that...
2.) The fact that on both pages you add the same style definition file does not mean the the wohle style definition is identical on both sites. Other style definitions might interfere. First, obviously the browsers builtin style rules might be different and then there might other style files be included. Such situations are always crusial to the order in which they are included. If some other style definition gets validated after your styles then the result may well come out different.
You will have to check the actual result you see using a browser builtin debugger.
It seems, that your problem has indeed to do with UTF-8. I analyzed your example pages with firebug, and it revealed, that the html pages contain the UTF-8 BOM-header, but the untranslated page contains it twice!
These 3 characters ( ) at the begin of the file are written by your editor, but they won't be displayed. With a HEX-editor you can see the difference, you can also go to the file properties and look at the file size, you will notice that they differ slightly.
My recommendation would be, to save the files as UTF-8 without BOM-header. Especially the double header makes the IE to switch to the quirks-mode (you can press F12 to get the developer tools in IE to see it), and that causes the different rendering. A double BOM-header is wrong in any case.
EDIT:
Just found a wonderful tool for checking utf-8 BOM and header problems.
If the same function with the same input is giving different output the function is a so called nondeterministic function.
Because if a function is non-deterministic, this is often not useful because you can not use it in a deterministic way (e.g. I have a problem A, I use function F than can deal with problem A, fine unless F is not clear what it does).
Therefore these uncertainties of making a function nondeterministic are often flaws which can lead to failure of the application. Or just different output as in your case.
This can happen because of hidden dependencies (e.g. there is an encoding parameter injected to a function you use inside that function that is specified via php.ini) or static state (the HTTP request differs so that the output of the function differs in the context of HTTP request and response of Browser and Server).
The function is not the problem here. On one of the pages, you specified the DOCTYPE and you forgot to do it on the other one. That's why it's interpreted by the browser differently. Remember to ALWAYS add the DOCTYPE to your pages (except when your code is in HTML5 and in XML notation).
So, add:
<!doctype html>
on the top of every page, so the rendering will be the same as on the http://mrhowtosay.com/translated page. Then apply some aesthetic changes if you need to. If you stick to pages with no DOCTYPE specified, you can never really be sure what will the output look like and there is a GREAT chance that it will be different on a different browser.

Why won't my image name convert to lowercase in PHP?

Here is the code I'm using:
<img src="/images/<?php strtolower(the_title()); ?>.jpg" border="0" >
Wordpress the_title() function echoes the title by default. You need to set the echo argument to false and echo the lowercase output yourself.
<img src="/images/<?php echo strtolower(the_title('','',false)); ?>.jpg" border="0" />
It looks like the_title() actually echo's out the title since you don't have an echo statement in your snippet. So your call to strtolower() is basically doing nothing. You'll need to capture the output of the_title() then you can convert it to lower case.
ob_start();
the_title();
$title = $template = ob_get_clean();
<img src="/images/<?php echo strtolower($title); ?>.jpg" border="0" />

Categories