Test of file existence always fails - php

<?
$user_image = '../images/users/' . $userid . 'a.jpg';
if (file_exists($user_image))
{
echo '<img src="'.$user_image.'" alt="" />';
}
else
{
echo '<img src="../images/users/small.jpg" alt="" />';
}
?>
Hello all, this code is supposed to check for a file and if it doesnt work, display another.
For some reason it is ALWAYS displaying the placeholder and never finds the initial file even though it is there.
Is there something obviously not right here?
Thanks for reading!

the PHP is running in a different directory. try echo getcwd();

file_exists does not work with relative paths. Try something like this:
$user_image = $_SERVER{'DOCUMENT_ROOT'}.'/../images/users/' . $userid . 'a.jpg';
if (file_exists($user_image))
// blah blah
But, as Artefacto suggests, it's better to use the real path:
$user_image = '/path/to/your/files/images/users/' . $userid . 'a.jpg';
It's easier to maintain since you can use that code on different PHP scripts located on different directories without having to change anything.

If your relative path points outside of the htdocs subdirectories, then the image will not be sent by the webserver

Try using realpath and dirname instead.
<?
$user_image = '../images/users/' . $userid . 'a.jpg';
if (file_exists(realpath(dirname(__FILE__) . $user_image)))
{
echo '<img src="'.$user_image.'" alt="" />';
}
else
{
echo '<img src="../images/users/small.jpg" alt="" />';
}
?>

I mean I'm not always the smartest with php, but is your concat location correct? Because right now, won't it resolve to /images/users/userida.jpg ? is this really what you want?

I think you should check to see what realpath('../images/users/' . $userid . 'a.jpg') returns. I get the feeling it has something to do with the relative path

Related

Is it possible to concatenate onto PHP $_SESSION variables?

I have an application that is using session variables. I am trying to pass the path of an image to another PHP page, but since the page is in another location my path breaks and the images won't display. Is there a way to concatenate onto the path in the $_SESSION variable?
page1.php
<?php
session_name('Private');
session_start();
$_SESSION['first_img'] = '<img src="img/first-img.png">';
$_SESSION['second_img'] = '<img src="img/second-img.png">';
?>
page2.php
<?php
session_name('Private');
session_start();
echo $_SESSION['first_img'];
echo $_SESSION['first_img'];
?>
since page2.php is in another folder, I would need to add ../ to the beginning of the src in the image path. I can't figure out how to add it to the $_SESSION variable.
one possible solution might consider storing the relative path in the session variable, and then wrapping the differences in your echo code.
page1.php
<?php
session_name('Private');
session_start();
$_SESSION['first_img'] = 'img/first-img.png';
$_SESSION['second_img'] = 'img/second-img.png';
?>
page2.php
<?php
session_name('Private');
session_start();
echo '<img src="../' . $_SESSION['first_img'] . '">';
echo '<img src="../' . $_SESSION['first_img'] . '">';
?>
Use absolute paths.
$_SESSION['first_img'] = '<img src="//'.$_SERVER["SERVER_NAME"].'/img/first-img.png">';
$_SESSION['second_img'] = '<img src="//'.$_SERVER["SERVER_NAME"].'/img/second-img.png">';

PHP not getting right link from path

Ok, so I have literally never been so confused. As you can see I have pretty much the same function twice here (I know that may seem stupid but it is just easier for me to read for my page when it's like this - but that isn't the point of this)
The first one goes to the link it's given (http://www.blade-edge.com/images/KSA/Flights/craft.asp?db=dunai) then follows the path to get the img src of http://i.imgur.com/8t5rwWh.png
But the second function doesn't get the src of the image it's pointing to (which would be http://i.imgur.com/jWWUEqt.png) but instead gets the src for a completely different image on the page http://www.blade-edge.com/images/KSA/Flights/prev.png.
I am sure this is a really stupid mistake that I have just overlooked but I can't work it out.
Anyone?
function getImage(){
$page = file_get_html(getPageURL());
$element = $page->find("html/body/div/div[1]/center/table/tbody/tr[1]/td/table/tbody/tr/td[1]/div/div/img");
$imgLink = $element[0]->getAttribute("src");
echo "<img id='shipImage' src='".$imgLink."'></img>";
}
function getMap(){
$page = file_get_html(getPageURL());
$element = $page->find("/html/body/div/div[1]/center/table/tbody/tr[2]/td/center/img");
$imgLink = $element[0]->getAttribute("src");
echo "<img id='shipMap' src='".$imgLink."'></img>";
}
The following works for me:
function getImage($imageType){
$page = file_get_html(getPageURL());
$element = $page->find("/html/body/div/div[1]/center/table/tbody", 0)->children($imageType)->find("img");
$imgLink = $element[0]->getAttribute("src");
return $imgLink;
}
echo "<img id='shipImage' src='" . getImage(0) . "'></img>"; // Spacecraft image
echo "<img id='shipMap' src='" . getImage(1) . "'></img>"; // Map image
I won't try to guess the reason behind the problem, as I do not know the innards of the library.

echo file if it exist. if not echo "default.png" php

I have a script that echo's usernames an inserts that into img src. This works great as long as the image is in the directory. How can I create an if statement that only echos the below command if the file exist? If it doesn't exist show default.png
I tried using mod_rewrite and have had zero luck with it..
<div class="contactphoto"><img src="contactphoto/<? echo ($note['user_name'] == "Support")? $note['first_name'].''.$note['last_name'] : $note['user_name'];?>.png"/></div>
The name says it all: file_exists()
I think this is what you want.
<?php
$file = ($note['user_name'] == "Support") ? $note['first_name'].''.$note['last_name'] : $note['user_name'];
$file .= '.png';
if(!file_exists($_SERVER{'DOCUMENT_ROOT'} .'/'.$file)){
$file = 'placeholder.png';
}
?>
<div class="contactphoto">
<img src="contactphoto/<?php echo $file; ?>"/>
</div>
If that fails, try a test ( this should match the path of the image ) also watch out for case sensitivity:
echo $_SERVER{'DOCUMENT_ROOT'} .'/'.$file;

check file exist with php isn't working, need help

Please advise me, what wrong with my following code:
<a href="<?php echo $_url; ?>" title="<?php echo $_name; ?>">
<?php
$logo2 = $_url.'/image/data/logo2.png';
$logo = $_url.'/image/data/logo.png';
if (file_exists($logo2)) {
echo "<img src=".$logo2." alt=\"Logo\" style=\"border: none;\" />";
} else {
echo "<img src=".$logo." alt=\"Logo\" style=\"border: none;\" />";
} ?>
</a>
both images of $logo2 and $logo exists in the same directory, but the code only shows $logo (logo.png)
I need pointers and thanks in advance
UPDATED:
the value of $_url is
$this->data['_url'] =
$this->config->get('config_url');
and when i <?php echo $_url;?> that will show e.g. http://www.mysite.com
by using code at above only show logo.png
file_exists can be used for URL wrapper.
In your case, if you really need to perform URL wrapper checking (will be very slow), make sure URL wrapper is enabled (default is enabled).
And also, your $_url = http://www.mysite.com///image/data/logo2.png, take note the extra slash may affecting web server rewrite.
If the file is located at the same server as your web server, you should replace the $_url to document_root (path to the folder).
For function wise, file_exists return true for directory too. You should replace that to is_file
You are applying file_exists() to a URL which doesn't work.
You need to apply it to a filesystem path.
file_exists expects a local path, not a url.
Contrary to some answers here, file_exists can take an URL as a parameter and it will check whether it exists or doesn't. However, you're still better off using a filesystem path for file_exists instead of the URL.
Anyway, two reasons immediately come to mind:
Do both files have the same permissions? (I.e., logo.png might have the necessary read permissions and logo2.png might not have them)
Are the file names really the same as in the script? For example, everything might work fine on your development platform - a Mac or Windows which ignores letter case for filenames but not on a Linux server where the filename must be in the same case.
Use getimagesize() as file_exists will return false.
<a href="<?php echo $_url; ?>" title="<?php echo $_name; ?>">
<?php
$logo2 = $_url.'/image/data/logo2.png';
$logo = $_url.'/image/data/logo.png';
if (getimagesize($logo2)) {
echo "<img src=".$logo2." alt=\"Logo\" style=\"border: none;\" />";
} else {
echo "<img src=".$logo." alt=\"Logo\" style=\"border: none;\" />";
} ?>
</a>

php - Decide whether an image exist and if it does show it

I have a code that shows a different image depending where on the page I am, but some places don't have an image so it displays a "no image" icon. I want to add a condition that checks if there really is an image in the given path and if returns false don't do anything. I have no idea how to do it.
This is the original code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr(get_search_query(), 1). ".jpg'>";
}
?>
What I need to know is which function do I use to get a true/false of that image path. Thanks
Use file_exists
$image_path = 'Imagenes/grupos/' . substr(get_search_query(), 1) . '.jpg';
if (file_exists($image_path)) {
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr(get_search_query(), 1). ".jpg'>";
} else {
echo "No image";
}
http://php.net/manual/en/function.file-exists.php
You can use file_exists

Categories