somebody knows why this script not working?
$imgname = get_stylesheet_directory_uri().'/images/headers/'.str_replace(' ', '', strtolower(get_the_title())).'.jpg';
if (file_exists($imgname)) {
echo '<img src="'.$imgname.'"> </img>';
} else {
echo '<img src="'.get_stylesheet_directory_uri().'/images/headers/default.jpg"> </img>';
}
it returns allways the default.jpg, even if the file exists
I checked the $imgname, and it is ok
yes you are right, I mus use like this:
$imgpath = get_stylesheet_directory().'/images/headers/'.str_replace(' ', '', strtolower(get_the_title())).'.jpg';
$imguri = get_stylesheet_directory_uri().'/images/headers/'.str_replace(' ', '', strtolower(get_the_title())).'.jpg';
if (file_exists($imgpath)) {
echo '<img src="'.$imguri.'"> </img>';
} else {
echo '<img src="'.get_stylesheet_directory_uri().'/images/headers/default.jpg"> </img>';
}
?>
You probably have to distinguish here between URLs and file paths.
When you direct your browser to a an image like
http://www.example.com/path/to/image.jpg
and it works, then still the file_exists() function will return false for this URL, because it's not an image path.
The correct path would be something like
/var/www/htdocs/path/to/image.jpg
on the local file system. file_exists() would then return true for this path.
What you need to test for with file_exists() is the local path of the image. If it exists, then you need to include the URL of the image. You do the URL inclusion correctly, but not the path usage.
Related
php is getting image in following format, while echoing $_POST['img']
http://localhost/uploads/images/1533033949-8.jpg
But why unlink doesn't working -
// Get src.
$img = $_POST["img"];
// Check if file exists.
if (file_exists(getcwd() . $img)) {
// Delete file.
unlink(getcwd() . $img);
echo "Deleted";
}
I tried testing directly, but doesn't work
unlink($img)
unlink works on the file system, not with HTTP URLs. And appending
#CBroe is correct
First get the base path on you live server
or manually specify the base path like below example
$base_directory = '/home/myuser/';
then unlink the file that you need to remove.
if(unlink($base_directory))
echo "File has been Deleted.";
I hope it helps.
Finally I solved storing url information as variable and php substr, strlen function.
$img=$_POST['img'];
$len = strlen("http://localhost/uploads/");
$new_path = substr($img, $len, strlen($img)-$len);
if(unlink($new_path)){
echo "Deleted";
}
else{
echo "Fail";
}
I'm facing a problem.
I have actually this code:
// Check if image exist for the hotel
$src = '../assets/app/images/hotel-logos/007.jpg';
if(file_exists($src)) {
$src = $src;
}
else {
$src = '../assets/app/images/hotel-logos/default.jpg';
}
echo '<center><img src="'.$src.'" width="200"></center>';
This code check for an image existence.
But each time I have the fallback image default.jpg whereas I should have 007.jpg.
I check my path and it works. My 007.jpg image is into the same directory as my default.jpg image.
I already test with if(#getimagesize($src)) { ... }. The same.
Why ?
Even that your file is placed at some directory, doesn't mean that the current path of the PHP process is the same. Use the absolute path instead:
// Check if image exist for the hotel
$src = '../assets/app/images/hotel-logos/007.jpg';
if(!file_exists(__DIR__.'/'.$src)) {
$src = '../assets/app/images/hotel-logos/default.jpg';
}
echo '<center><img src="'.$src.'" width="200"></center>';
I think you are over doing it make it simpler might solve your issue Try This:
<?php
$src = '../assets/app/images/hotel-logos/007.jpg';
if (!file_exists($src)) {
$src = '../assets/app/images/hotel-logos/default.jpg';
}
echo '<center><img src="'.$src.'" width="200"></center>';
?>
Ok, I get it working by using:
$_SERVER['DOCUMENT_ROOT']
// Check if image exist for the hotel
$src = '../assets/app/images/hotel-logos/007.jpg';
if(!file_exists($_SERVER['DOCUMENT_ROOT'].'/assets/app/images/hotel-logos/007.jpg')) {
$src = '../assets/app/images/hotel-logos/default.jpg';
}
echo '<center><img src="'.$src.'" width="200"></center>';
Here is my code:
<?php $filename = $var.'p_folder/'.sub_replace('?','',$page).'/images/default.png'; ?>
<img src = "<?php echo $filename; ?>"
title= "<?php echo file_exists($filename) ? 'exists' : 'not exist'; ?>"
>
My code shows the image as well, but file_exists() returns false (I mean "not exist" prints).. Why?
Actually that's pretty much odd for me .. because I can see the image on the web, so it means the image exists on the directory, but why file_exists() cannot find it?
file_exists() needs to use a file path on the hard drive, not a URL. So you should have something more like:
$thumb_name = $_SERVER['DOCUMENT_ROOT'] . 'images/abcd.jpg';
if(file_exists($thumb_name)) {
//your code
}
check your image path and then sever name & document root
I'm working on Wordpress at the moment and as simple as it's sounds i'm trying to check if the image is there in the directory if not then it will show a standard no-image.
My problem is that the file_exists is returning false although the url is correct inside it, and it's accessible via browser so it's not a permission issue, and maybe i'm doing it wrong, here's the code;
$img = 'no_img.jpg';
if($val->has_prop('user_image')){
$img_tmp = $val->get( 'user_image' );
if(file_exists($upload_dir['baseurl'].'/users/'.$img_tmp)){
$img = $val->get( 'user_image' );
}
}
if i do var_dump($upload_dir['baseurl'].'/users/'.$img_tmp); it will show the exact direct URL to the file, and it's correct one, but when it enters the file_exists it returns $img = 'no_img.jpg' although the file exists in the directory.... What am i doing wrong??
I tried also to add clearstatcache(); before the file_exists but didn't work also.
Any ideas?
Try to use:
if( getimagesize($upload_dir['baseurl'].'/users/'.$img_tmp) !== false ){
$img = $val->get( 'user_image' );
}
else {
$img = $val->get( 'no_image' );
}
Also refer to the doc getimagesize
you can use
$fullPath=$upload_dir['baseurl'].'/users/'.$img_tmp;
if(#fopen($fullPath,"r")){
........
}
Or as mentioned in comments, try sniff instead :
if (#file_get_contents($upload_dir['baseurl'].'/users/'.$img_tmp, null, null, 0, 1)) {
//ok
}
You need to use $upload_dir['basedir'] instead of baseurl if you used $upload_dir = wp_upload_dir(); to determine the upload path of your WP installation.
or you can use file_get_contents, cURL, or fopen to sniff if image exists for the url.
I am able to get the web path to the file like so:
$filename = 'elephant.jpg';
$path_to_file = $this->getSkinUrl('manufacturertab');
$full_path = $path_to_file . '/' . $filename;
But if the file doesn't exist, then I end up with a broken image link.
I tried this:
if(!file_exists($full_path)) {
Mage::log('File doesn\'t exist.');
} else {
?><img src="<?php echo $full_path ?>" /><?php
}
Of course that didn't work because file_exists does not work on urls.
How do I solve this?
1.)
Can I translate between system paths and web urls in Magento?
e.g. something like (pseudocode):
$system_path = $this->getSystemPath('manufacturertab');
That looks symmetrical and portable.
or
2.)
Is there some PHP or Magento function for checking remote resource existence? But that seems a waste, since the resource is really local. It would be stupid for PHP to use an http method to check a local file, wouldn't it be?
Solution I am currently using:
$system_path = Mage::getBaseDir('skin') . '/frontend/default/mytheme/manufacturertab'; // portable, but not pretty
$file_path = $system_path . '/' . $filename;
I then check if file_exists and if it does, I display the img. But I don't like the asymmetry between having to hard-code part of the path for the system path, and using a method for the url path. It would be nice to have a method for both.
Function
$localPath = Mage::getSingleton( 'core/design_package' )->getFilename( 'manufacturertab/' . $filename, array( '_type' => 'skin', '_default' => false ) );
will return the same path as
$urlPath = $this->getSkinUrl( 'manufacturertab/' . $filename );
but on your local file system. You can omit the '_default' => false parameter and it will stil work (I left it there just because getSkinUrl also sets it internaly).
Note that the parameter for getSkinUrl and getFilename can be either a file or a directory but you should always use the entire path (with file name) so that the fallback mechanism will work correctly.
Consider the situation
skin/default/default/manufacturertab/a.jpg
skin/yourtheme/default/manufacturertab/b.jpg
In this case the call to getSkinUrl or getFilename would return the path to a.jpg and b.jpg in both cases if file name is provided as a parameter but for your case where you only set the folder name it would return skin/yourtheme/default/manufacturertab/ for both cases and when you would attach the file name and check for a.jpg the check would fail. That's why you shold always provide the entire path as the parameter.
You will still have to use your own function to check if the file exists as getFilename function returns default path if file doesn't exist (returns skin/default/default/manufacturertab/foo.jpg if manufacturertab/foo.jpg doesn't exist).
it help me:
$url = getimagesize($imagepath); //print_r($url); returns an array
if (!is_array($url))
{
//if file does not exists
$imagepath=Mage::getDesign()->getSkinUrl('default path to image');
}
$fileUrl = $this->getSkinUrl('images/elephant.jpg');
$filePath = str_replace( Mage::getBaseUrl(), Mage::getBaseDir() . '/', $fileUrl);
if (file_exists($filePath)) {
// display image ($fileUrl)
}
you can use
$thumb_image = file_get_contents($full_path) //if full path is url
//then check for empty
if (#$http_response_header == NULL) {
// run check
}
you can also use curl or try this link http://junal.wordpress.com/2008/07/22/checking-if-an-image-url-exist/
Mage::getBaseDir() is what you're asking for. For your scenario, getSkinBaseDir() will perform a better job.
$filename = 'elephant.jpg';
$full_path = Mage::getDesign()->getSkinBaseDir().'/manufacturertab/'.$filename;
$full_URL=$this->getSkinUrl('manufacturertab/').$filename;
if(!is_file($full_path)) {
Mage::log('File doesn\'t exist.');
} else {
?><img src="<?php echo $full_URL ?>" /><?php
}
Note that for the <img src> you'll need the URL, not the system path. ...
is_file(), rather than file_exists(), in this case, might be a good option if you're sure you're checking a file, not a dir.
You could use the following:
$file = 'http://mysite.co.za/files/image.jpg';
$file_exists = (#fopen($file, "r")) ? true : false;
Worked for me when trying to check if an image exists on the URL