I'm trying to make profile photos show up against a list of reviews on a site I'm working on. If they don't have a profile photo I have a standard image to show instead, unfortunately the image always goes to the standard image rather than the profile even if it exists. Heres the code:
$reviewerPic = 'http://www.[URL].co.uk/images/members/' . $reviewPosterId . '/profilePic.jpg';
$default_pic = 'http://www.[URL].co.uk/images/background.jpg';
if(file_exists($reviewerPic)){
$reviewer_pic = '<img src="' . $reviewerPic . '" width="100px" style="float: left; margin: 20px;" />';
}else {
$reviewer_pic = '<img src="' . $default_pic . '" width="100px" style="float: left; margin: 20px;" />';
}
Pretty generic code but it doesn't seem to work! It just keeps showing the background image...
Any ideas on why file_exists wouldn't be working?
The function you are using, file_exists, uses physical paths, the parameter you need to provide should be the address on that server where the file can be found, and not an url
Sou you should have something like
/home/var/www/images/
instead of
http://www.[URL].co.uk/images/
So you need to check if the file exists on the server locally and after that you can use an url to make it available to the public (in img src)
You can see on the man page that this function only works with some URL wrappers, so it is better to use paths and not urls (I guess it depends on allow url fopen setting)
file_exists does not work with HTTP,
use : $_SERVER["DOCUMENT_ROOT"] to access root in your host
so
$reviewerPic = $_SERVER["DOCUMENT_ROOT"].'/images/members/' . $reviewPosterId . '/profilePic.jpg';
$default_pic = $_SERVER["DOCUMENT_ROOT"].'/images/background.jpg';
File_exists uses the /home/www/username/public_html/ format. You may want to retrieve this from your host.
So it would be something like /home/www/jack/public_html/[URL].co.uk/profilepic/id/profilePic.jpg.
By the way, no need to use . in your echo '<img src=... . You can use variables inside of strings, you should only use the . when you need to modify it using a function or something, like "string0".function($str....)."string1";
According to the PHP manual for file_exists not all protocols supports the function:
this function can also be used with some URL wrappers. Refer to
Supported Protocols and Wrappers to determine which wrappers support
stat() family of functionality.
The HTTP wrapper does not support stat:
Supports stat() No
the file_exists PHP function works only on physical files like /var/www/file.ext or C:\WWW\File.ext and not for HTTP Files. It doesn't even know how to handle HTTP files.
In this case, if you want to know if the file is there or not, since it is going to be an HTTP Response, you can try using curl, which if it responds with 404, then the file is not found.
For more information, check out these: http://php.net/manual/en/function.file-exists.php and
http://php.net/manual/en/book.curl.php
file_exists doesn't support remote URLs.
You can try this:
if (fopen($reviewerPic, "r"))
echo "File exists!";
Try simple one.
$reviewerPic = 'http://www.[URL].co.uk/images/members/' . $reviewPosterId . '/profilePic.jpg';
$default_pic = 'http://www.[URL].co.uk/images/background.jpg';
if($fileopen = #fopen($reviewerPic)){
$reviewer_pic = '<img src="' . $reviewerPic . '" width="100px" style="float: left; margin: 20px;" />';
fclose($fileopen);
}else {
$reviewer_pic = '<img src="' . $default_pic . '" width="100px" style="float: left; margin: 20px;" />';
}
A great way to handle it is here ~
<?php
$imgok="1.webp"; // Your actual image
$imgerr="template.webp"; // If image is missing
$dir="/assets/image/products/"; // Location of the image directory
$path = $_SERVER['DOCUMENT_ROOT'].$dir. $imgok; // Full Paths for processing
echo file_exists($path) ? $dir.$imgok : $dir.$imgerr; // Actual paths for domain to get the image
?>
Related
I have a small problem of needed to check which of 3 possible file paths relates to the correct image. I've always used get_headers for this in the past - but there are up to 100 images to load on any given pagination - so thats a potential of 300 get_headers in a worst case scenario and its painfully slow on my university server space.
However for any given steam_id there is the potential for images to exist at 2 of the possible file paths. Which means if I just include all three of urls and use Jquery to hide the on errors, for around half the results I end up with 2 images (which is in all fairness faster then what I'm doing currently, but at least the get_headers method looks fine.
Here's what I'm using so far:
//Use Header Response to determine which img to use
$url1 = 'http://cdn4.steampowered.com/v/gfx/apps/' . $row -> steam_id_clean . 'header_292x136.jpg';
$url2 = 'http://cdn4.steampowered.com/v/gfx/apps/' . $row -> steam_id_clean . 'header.jpg';
$header_response = get_headers($url1, 1);
if (strpos($header_response[0], "404") !== false) {
// File does not exist at url1 - recurse.
$header_response = get_headers($url2, 1);
if (strpos($header_response[0], "404") !== false) {
//File does not exist at URL - Therefore must be at URL3
echo '<img src = "http://cdn4.steampowered.com/v/gfx/subs/' . $row -> steam_id_clean . 'header.jpg" style="width: 200px; height: auto;" >';
} else {
//File exists at URL2
echo '<img src = "http://cdn4.steampowered.com/v/gfx/apps/' . $row -> steam_id_clean . 'header.jpg" style="width: 200px; height: auto;" >';
}
} else {
// File exists at URL1
echo '<img src = "http://cdn4.steampowered.com/v/gfx/apps/' . $row -> steam_id_clean . 'header_292x136.jpg" style="width: 200px; height: auto;" >';
}
This data is too dynamic - so I can't write a scrape to grab the images. Any Suggestions in a better method than get_headers would be appreciated. I don't have access to curl (unfortunately) on my university server either!
I am creating a Google chart in wordpress, chart is rendered and stored into an image. However I am not able to call the image using <img src="" />. Following is the code:
$filepath = "/wp-content/uploads/graph.png";
file_put_contents($filepath, $response);
echo $filepath;
echo "<img src=\"/wp-content/uploads/graph.png\">";
I have also tried with http://*/graph.png which is not working. If I open the same in different browser, image is showing properly.
Try this
echo '<img src="' . get_bloginfo('template_directory') . '/images/logo.gif" />';
Go for 'template_directory' or 'stylesheet_directory'.
You may try the below code;
Much better if you combine your HTML code and PHP code. Much cleaner easy to read.
PHP code
<?php
Try changing your file path to
$filepath = "../../wp-content/uploads/graph.png";
file_put_contents($filepath, $response);
?>
HTML Code
<img src="<?php echo $filepath; ?>"/>
I have an HTML file that has external assets on a secure server that needs a url generated in order to load. I can load the HTML file fine, but none of the images will load. Relative paths wont work. I need to pass the image src attribute to my generateURL method and replace the current relative src paths with my generated url. An example HTML file I need to replace the tags in would be:
<img id="Im0" src="slide1page1/img/1/Im0.png" alt="Im0" width="720" height="540" style="display: none" />
<img id="Im1" src="slide1page1/img/1/Im1.png" alt="Im1" width="136" height="60" style="display: none" />
<img id="Im2" src="slide1page1/img/1/Im2.png" alt="Im2" width="669" height="45" style="display: none" />
My php script that I load the page through is:
<?php
function generateURL($target,$seconds)
{
$secret = "mysecretpasscode";
$end = time() + $seconds;
$url = $target . "?e=" . $end;
$toHash = $secret . $url;
$secure = $url . "&h=" . md5($toHash);
return $secure;
}
$id = $_POST['id'];
$loc = $_POST['loc'];
$url = $loc . "slide" . $id . ".html";
$secure = generateURL($url,600);
$page = file_get_contents($secure);
?>
if I echo $page, I can see the page as if I just loaded an html page (but no images). What I need to do before that is find all the src tags in the HTML and run them through my generateURL method so I can generate the correct URL with proper hashing to load. I am thinking a str_replace will work, but I am not familiar enough with regex to continue. Any help would be appreciated.
First have a look at this answer: RegEx match open tags except XHTML self-contained tags
Though I think you could use PHP's preg_replace_callback as follows:
src_regex = "/src=\"([^\"]+\/";
preg_replace_callback(src_regex, generateURL, html);
Do take into account that the second parameter (generateURL) must be a function with only a single argument, take a look at this page.
A html parser would be better. Provided you've selected all the tags, you can extract the relative path with the function below.
$u = '<img id="Im0" src="slide1page1/img/1/Im0.png" alt="Im0" width="720" height="540" style="display: none" />';
$v = explode(" ", $u);
$x = explode("=", $v[2]);
echo str_replace("\"", "", $x[1]);
Outputs
slide1page1/img/1/Im0.png
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>
<?
$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