echo file if it exist. if not echo "default.png" php - 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;

Related

if file_exists in PHP issue wordpress

I am attempting to create code that checks if an image exists on my site and if not shows a default image. In one case I know the file exists and can get it to link to the file using the variable that I want file_exist to use!
$menuCategories = get_categories( array(
'child_of' => $whichGrade,));
foreach ( $menuCategories as $menuCategory ) { ?>
<?php
$linktoicon = get_bloginfo('template_directory') ."/images/menuicon_".$menuCategory->slug.".png";
if (file_exists($linktoicon)) {
$iconref = $menuCategory->slug;
}else {
$iconref = "default";
} ?>
<a href='<?php echo $linktoicon; ?>'> <?phpvar_dump($iconref); ?></a>
<?php }?>
$linktoicon` is "http://mybritishhelper.com/wp-content/themes/wpex-magtastico/images/menuicon_colours.png"
Thank you.
PHP's file_exists isn't used with URL's, it's used for paths to files on the server itself. Not a problem. First, we need to get the path to your theme's template directory:
$templateDirectory = get_template_directory();
Assuming that the link you provided is on your own server, the full path to your image is
$pathToImage = $templateDirectory . '/images/menuicon_colours.png';
We can now check if your image exists with the following
if (file_exists($pathToImage)) {
// Do stuff
}
Hope this helps. For reference, see the docs for file_exists and get_template_directory()

PHP: if image exists show image else show different image

i am making a login system with registration and a profile page in php and i am trying to make a profile picture work.
if the user has not uploaded a profile picture yet then make it show a "no profile picture" image if the user has uploaded a profile picture make make it show the image that he has uploaded.
Right now it only show the default picture, noprofile.png.
< img src="uploads/< ? echo "$username" ? >/noprofile.png">
i want it to show icon.png if icon.png has been uploaded and if it hasnt been uploaded make it show, noprofile.png.
Just run it through the logic, using file_exists:
$image="/path/on/local/server/to/image/icon.png";
$http_image="http://whatever.com/url/to/image";
if(file_exists($image))
{
echo "<img src=\"$http_image\"/>\n";
}
else
{
echo "<img src=\"uploads/$username/noprofile.png\"/>\n";
}
Check to see if the file has been uploaded by using file exists. If the file exists, use that url else use the default noprofile.png.
you could make a column in the DB to store a value if it has been uploaded or not.
OR
you could see if the file exists.
<?php
if (file_exists('uploads/' . $username . '/icon.png')) {
echo '<img src="uploads/' . $username . '/icon.png">';
}
else {
echo '<img src="uploads/' . $username . '/noprofile.png">';
}
?>
<?php
$img = file_exists(sprintf('/path/to/uploads/%s/icon.png', $username))
? 'icon.png' : 'noprofile.png';
?>
<img src="uploads/<?php printf('%s/%s', htmlspecialchars($username), $img) ?>">
You could use http://us3.php.net/file_exists to check if the image file is there.
Another alternative is - assuming you keep your user info in a database - have a column with the image name. Since you have to retrieve info from your user table anyway, check to see if that column is NULL or blank. If it is, the user has not uploaded an image yet.
Then, in the page you display the user photo, you might have code something like this:
$userPhoto = ($photoName)? $photoName : 'placeholder';
echo '<img src="uploads/'.$userPhoto.'.png" />
Assuming the filepaths are correct, here's what you do...
<?php $filename = "uploads/".$username;
$imgSrc = file_exists($filename) ? $filename : "uploads/noprofile.png"; ?>
<img src=<?php echo $imgSrc?>
Use onerror attribute in img tag
<img onerror="this.src= 'img/No_image_available.png';" src="<?php echo $row['column_name ']; ?>" />

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

Test of file existence always fails

<?
$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

Categories