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>
Related
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()
I am using a simple script that displays images in a jquery slideshow - these image filenames are listed in a .txt file, and change depending on the page you are on (im also using pagination in another script).
If the filename that is listed in the .txt file doesn't exist, I would like the image 'unavailable.jpg' to display instead...
The original script:
<?php
echo"
<div id='slider-wrapper'><div id='slider' class='nivoSlider'>";
$photos=file("photos.txt");
foreach($photos as $image){
$item=explode("|",$image);
if($item[0]==$fields[0]){
$photo=trim($item[1]);
echo"<img src='images/work/$photo' alt='' />\n";
}
}
echo"
</div>
</div>
"?>
And here is my try at it...but it doesn't work properly- instead of the 'unavailable.jpg' image being displayed, it shows all of the images in the directory... :S Anyone have any ideas of what I might be doing wrong? :S
<?php
echo"
<div id='slider-wrapper'><div id='slider' class='nivoSlider'>";
$photos=file("photos.txt");
foreach($photos as $image){
$item=explode("|",$image);
$photo=trim($item[1]);
if (file_exists("images/work/".$photo)) {
echo"<img src='images/work/$photo' alt='' />\n";
}
else{
echo"<img src='images/work/unavailable.jpg' alt='' />\n";
}
}
echo"
</div>
</div>
"?>
Instead of all the images showing, I only want images for that page to display. Here is an example of my text file:
1|image1.jpg
1|image2.jpg
1|image3.jpg
2|image1.jpg
2|image2.jpg
The 1 and 2 are for the pages 1 and 2, and they display the images that are listed. This all works fine in the above original script that I have posted, but it seems to break when I add the if file_exists.
The path to your images uses a relative path. Are you sure the current working directory is the directory you think it is?
To verify do an echo 'Current Working Directory: '.getcwd()."<br />\n" and verify what directory you are in.
Its probably best to use a full file path to your image's directory so the script can be placed anywhere on your server.
Now if that is correct then you need to check that your script has permission to your image directory. Typically php runs as nobody:nobody or apache:apache depending on your configuration.
The directories above as well as the files should have 644 (-rw-r--r--) or at a minimum 444 permission (-r--r--r--).
Try these two things and let us know if that solved your specific problem or not; I hope it does.
You need to re-add the test for $item[0] == $fields[0]:
<?php
echo "<div id='slider-wrapper'><div id='slider' class='nivoSlider'>";
$photos = file("photos.txt");
foreach ($photos as $image) {
$item = explode("|",$image);
$photo = trim($item[1]);
if (file_exists("images/work/".$photo)
&& $item[0] == $fields[0]
) {
echo "<img src='images/work/$photo' alt='' />\n";
} else {
echo "<img src='images/work/unavailable.jpg' alt='' />\n";
}
}
echo "</div></div>";
?>
Edit:
I just noticed that Ott pointed this out already in the comments. Are you still having the issue with unavailable.jpg?
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;
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
<?
$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