I am using codeigniter, I want to show a user page with his profile picture shown. When a user uploads a profile picture, it will get the same name as the username (where the old profile picture will be overwritten). If he hasn't uploaded a profile picture, then a questionmark picture is shown. I use file_exists function, but it returns every time false.
$filename = base_url().'images/userimg/'.$this->session->userdata('username');
if(file_exists($filename)){
echo '<img src="'.$filename.'" width="300" height="300" /><br/>';
}
else {
echo '<img src="'.base_url().'images/no_image.jpg" width="300" height="300" /><br/>';
}
Change your codes into this:
$filename = './images/userimg/'.$this->session->userdata('username');
It could be that your problem results from thae usage or url instead of local server path. That base_url in the config file contains an URL and the file_exists function should work with a full local path - for which you can use FCPATH (like Svetlio suggested) or any other way. Although the file_exists should work with URL , i think there will be more trouble with it than a full path.
Related
I've got the broken images when I try to retrieve all jpg files from the folder where the name is 'imgProduct'.
The error message is 'Failed to load resource: the server responded with a status of 404 (Not Found)'
The directory of all images is C:\Sungsan\imgProduct.
And the directory of code below is C:\Sungsan\Website\view\shopPanel.php.
<?php
$productImg = glob("../imgProduct/*.jpg*");
echo var_dump($productImg).'<br>';
for($a = 0; $a < count($productImg); $a++) {
$num = $productImg[$a];
echo basename($num)."<br />";
echo '<img src="' .$num. '" alt="random image"> <br />';
}
?>
This image is the result that code above is excuted.
I was searching for how to troubleshoot this broken images but I have no idea. Hope to give recommendation or suggestion. Thanks
Your basepath makes ../imgProduct/img01.jpg to be img01.jpg. And I assume that your web root is not inside imgProduct, so you need to append it:
$num = basename($num);
echo "{$num}<br/><img src='/imgProduct/{$num}' alt='random image'> <br />";
Also don't mistaken server-side path with web-side url. ../imgPorduct/img01.jpg is going back in your URL from current website page, not from PHP file
I could be wrong, but by the looks of it you're trying to reference files in the wrong directory?
Change your image src to point to the folder containing the images (currently it's seemingly looking for 'img1.jpg' in the web pages current directory.
echo '<img src="../imgProduct/' .$num. '" alt="random image"> <br />';
After looking at your screenshot again and reading some of the comments, I've also noticed that you could likely also just remove basename as it is stripping out the rest of the file path.
Hope this helps.
I have seen some similar questions here, but no answer fit my needs.
I have a Wordpress and in the upload dir there is an image, I have the following url for the image: ../../uploads/2016/2/56c3620a9c8af.png I try to access the file from inside the child theme folder:
/home/mydomain/www/wp-content/themes/twentyfifteen_child
I would like to check if the file exists and afterwards unlink() the image, but the file_exists always returns false, even though I can echo the image.
The following simple function outputs the image but returns false.
function checkImageExist($url)
{
echo '<img src = "'.$url.'" /><br>'; //The image is rendered
clearstatcache();
if(file_exists($url))
{
echo 'Image exists '.$url;
}
else
{
echo 'Image does not exist';
}
}
And this is what I CAN'T UNDERSTAND.
What am I missing?
THERE WAS NO QUESTION AT ALL:
The script I was using to upload the files was duplicating the extension names, so 56c3620a9c8af.png was in fact 56c3620a9c8af.png.png this stupid mistake was the responsible of images appearing when called (as the browser was able to parse them even though the duplicated extension) but file_exists was not able to find them.
So everything was working and I was mistaken about the error source.
You are passing a $url when what you really need is a $path. You can get the path like this:
$path = wp_upload_dir(); //This returns an array with URL info
$path['path']. '/2016/2/56c3620a9c8af.png'
Am wondering why these image isn't showing even after properly retrieving the image path from MYSQL i've gone through the code and i still can't figure out why this is:
<?php
$sql = "SELECT description, quantity, product_price, category, product_img_url";
$sql .= " FROM *****, *****";
$sql .= " WHERE product_id = product_img_id";
$sql .= " AND product_id = ?";
$sql .= " LIMIT 1";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s', $id);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows==1){
$stmt->bind_result($desc,$qty,$pr,$cat,$product_img_url);
$stmt->fetch();
?>
<div id="infobox">
<h3>Product View</h3>
<p align="left">
<?php
if(file_exists($product_img_url)){
echo '<img src="'.$product_img_url.'" alt="" width="300" height="300" />';
}else{
echo '<img src="" alt="product image" />';
}
?>
Please can someone tell me why this is happening?
If the image file does exist, then this might have something to do with the path.
I would use firebug to play with the image src path and try various things like putting a slash in the beginning, or removing it, or entering absolute path of image.
Once I get the image to show up in firebug, I would then proceed to fix the issue in the code.
As i am see you code and i just want to ask you that you have store complete path of your image in db or you have store just some part of it.
as i can see you have retrieve image path from mysql but the browser not able to retrieve image from your server path it may be because of path you r providing and and where the file store not match. the best way to check your file path and mysql path use
var_dump(your result set) and check you output weather image url match with your server image path, and i am prety much sure that your path will not match.
Can you please try with the following path structure
echo '<img src="/application_root_directory/'.$product_img_url.'" alt="" width="300" height="300" />';
Well, You can do like:
$product_img_url="guarantee.png"; ?>
<img src="images\<?php echo $product_img_url; ?>" alt="Hello">
It will not installed by default you have to install it manually. Either you can do goto tools in menubar>add-ons, and search in search fox for firebug or goto firebug website to install it.
Thanks everyone for all useful ideas presented, I appreciate. I've found a way to resolve the relative/absolute links. The images are now being displayed as they should! What I did was; since I have sub directories under my root folder, I created separate 'Meta' php scripts in each sub directory with names like INC, __ADM, etc. Each script links to another script in a toplevel directory. All I placed within the scripts were just paths, that I got using DOCUMENT_ROOT, doing this I was able to get the actual path to the image folder, that I then placed in the DB. Hope this idea helps someone else!
I have an image url but there is not image or the image name has been renamed, so i am not able to view the image in this case i want to show a default custom image. any idea on how to do it
thanks in advance
If it's the same site, you can use any of PHP filesystem functions to see if an image file still on it's place. is_readable() is my favorite one for that purpose.
Sure, not URL but a filesystem path should be used.
If it's just a hotlinks to other sites - forget it. You can't check it for reasonable price.
if the image is one your server ie. if you know the path to the image file, you can use the php function file_exists
if (file_exists($imageFilePath)){
$url = $imageUrl;
} else {
$url = $customImageUrl;
}
if the file is located on your server
<?php
$filename = '/path/to/file.jpg';
if (!file_exists($filename)) {
$filename = '/path/to/default.jpg'
}
?>
otherwise you can try using GetImageSize
if(#GetImageSize($remoteImageURL)){
//image exists!
}
I'm writing a wordpress plugin, and using this script to resize images: Timthumb
This script uses absolute paths, but I can't get it to work for me; I've triple-checked all my paths but still nothing.
Here is my code:
$plugin_dir_name = "my-plugin";
$pathTimThumb = WP_PLUGIN_URL . '/' . $plugin_dir_name . '/timthumb.php';
$pathToUpload = WP_CONTENT_URL.'/uploads/'.$plugin_dir_name;
$hImg = 150;
$wImg = 150;
....
$myImage = '<img class="thumb" src="'.$pathImageThumb.'?src='.$pathToUpload.'/'.$allImages[$i].'&h='.$hImg.'&w='.$wImg.'&zc=1" alt="">';
In firebug I get this URL:
<img alt="" src="http://localhost/mu/wp-content/plugins/my-plugin/timthumb.php?src=http://localhost/mu/wp-content/uploads/my-plugin/car___1/26zhoar5.jpg&h=150&w=150&zc=1" class="thumb">
Where is the mistake?
use this one.
$my_plugin_url = plugins_url('my-plugin-name/');
$my_timthumb_url = $my_plugin_url.'timthumb.php?';
$my_image_url = 'http://localhost/images/image.jpg';
echo '<img alt="" src="'.$my_timthumb_url.'src='.$my_image_url.'&h=150&w=150&zc=1"/>';
things to consider to make timthumb work:
chmod your timthumbs cache folder
do not use external images
check you timthumb version
Cheers,Dave
WP_CONTENT_URL is a url, not an absolute path. Use WP_CONTENT_DIR instead.
TimThumb tries to determine the local path of the image by stripping out http://CURRENT_HOST.tld from the beginning of the src parameter.
Since you're running on localhost it might be getting a little confused and falsely calculating it as an external image. I doubt this is the case (I checked the source and it should be OK), but it's an educated guess.
Have you tried reading the HTTP response headers from http://localhost/mu/wp-content/plugins/my-plugin/timthumb.php?src=http://localhost/mu/wp-content/uploads/my-plugin/car___1/26zhoar5.jpg&h=150&w=150&zc=1?
If not, use HttpFox for FireFox and post back the results.