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
Related
anybody can help to solve this problem. i have 2 files php :
file list folder which send $_REQUEST as Link
example : http://localhost/sertifikat/bacafolder.php?Link=%20Cipto%2075%20HGB%201159%20207%20M2
file php for execute and OPENDIR that $Link.
but its can't work, even though that Folder is exist
this is the second file php :
<?php
$Link=$_REQUEST['Link'];
$dir= $Link;
echo $dir;
if($opendir=opendir($dir))
{while(($file=readdir($opendir)) !=FALSE)
{
echo "<img src='$dir/$file'><br>";
}
}
?>
I was trying to use google APIs, and on process, I got an error, saying file is missing.
On trying to access the file, which exits, file_exists() returns false, no idea where I'm going wrong.
Note: It is in local host, using Xampp.
The result on execution:
Image of the directory:
File.php:
<?php
clearstatcache();
$dir= __DIR__."\client_secret.json";
if (file_exists($dir)){
echo "exists";
} else {
echo "doesn't exist at ".$dir;
}
?>
Please help me find where I have gone wrong.
Do a dir on that folder from dos and check that your file is not named
client_secret.json.json
Windows file explorer displays the name of the file without the extension. There for your file name is client_secret.json with the extension of .json
<?php
clearstatcache();
$dir= __DIR__."\client_secret.json.json";
if (file_exists($dir)){
echo "exists";
} else {
echo "doesn't exist at ".$dir;
}
?>
Because your File.php and client_secret.json is located in same file It will work.
try this
<?php
clearstatcache();
$dir= "client_secret.json";
if (file_exists($dir)){
echo "exists";
} else {
echo "doesn't exist at ".$dir;
}
?>
As client_secret.json file is in the same path as file.php, you can directly do
if(file_exists("client_secret.json")){ //file name without whole path
echo "Exists";
}else{
echo "Not Found!";
}
where is the file.php path ?
__DIR__will find the file.php directory ,so you can echo the $dir value , and then check the path is ok ?
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.
I have a script that uploads some files through php's move_uploaded_file().
Tested on my localhost it works fine. The problem arises when trying to do the same thing on a host. I already red all the topics in that matter here - but none of them solved my problem.
CODE:
<?php
$folder = 'img';
if (isset($_FILES['test'])) {
if (is_writable($folder))
echo 'Writable';
else
echo 'IMG is not writable';
$tmp_name = $_FILES['test']['tmp_name'];
$name = $_FILES['test']['name'];
if (move_uploaded_file($tmp_name, $folder . '/' . $name)) {
echo 'File was uploaded';
}
else {
echo 'File was not uploaded';
}
}
else {
echo 'No file - no operation';
}
?>
<html>
<body>
<form action="" enctype="multipart/form-data" method="POST">
<input type="file" name="test" />
<input type="submit" value="Test" />
</form>
</body>
</html>
MESSAGE is as follows:
Writable
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\WINDOWS\Temp\php9E75.tmp' to 'img/20130113_114901.jpg' in F:\hshome\ctc-ultralife\ultralife.com\new\admin\index_files\function\test2.php on line 11
File was not uploaded
Folder seems writable. Even if the file is not uploaded, it is created with the specified name in the folder (even if it is an empty file).
I do not know the absolute path of the server - it is not mine.
Your help will be much appreciated.
Thanks in advance.
Warning: complete stab in the dark. I don't know if this is the case, as I don't work on Windows servers.
It may be that the / needs to be a \. I don't think Windows handles the slash being the other way. That would explain why it can find your directory, but not the file within it.
I think you have to give an absolute path to the move_uploaded_file function.
EDIT:
If you don't know the absolute path, you can use the __DIR__ constant to make up an absolute path.
Assuming you've the img folder in the function folder, you can write something like this:
$fullPath = __DIR__ . '/img/' . $name;
if (move_uploaded_file($tmp_name, $fullPath)) {
echo 'File was uploaded';
} else { // ... }
In order to understand the real problem, you've to look into the error log.
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