Reputation: 12327
I want to check if a img file exist else i use a default img.
but the check i want to use to be sure the image file is there dosnt work.
I have the following code.
$filename = "http://".$_SERVER['SERVER_NAME']."/media/img/".$row['CatNaam'].".jpg";
echo" <img src=\"".$filename."\" alt=\"".$row['CatNaam']."\">";
echo "filename";
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
the image is there i can see it, but it says that the image does not exist. if i copy the response from echo filename the file is there.
EDIT:
i changed my code to
$filename = "/media/img/".$row['CatNaam'].".jpg";
echo $filename;
echo" <img src=\"".$filename."\" alt=\"".$row['CatNaam']."\">";
echo "<br> $filename <br>";
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
I can still see the image but now i get a diffrent warning ( i gues this is better then before)
the warning says:
arning: file_exists() [function.file-exists]: open_basedir restriction in effect. File(/media/img/Badkraan.jpg) is not within the allowed path(s): (censored) in mydomain/public_html/ve/paginas/producten/zoek.php on line 71 The file /media/img/Badkraan.jpg does not exist
The filepermission is 755
Upvotes: 0
Views: 6549
Reputation: 667
$filename = "http://im.rediff.com/money/2011/jan/19sld3.jpg";
$file_headers = @get_headers($filename);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
//return false;
echo "file not found";
}else {
//return true;
echo "file found";
}
Upvotes: 0
Reputation: 20452
Source : https://www.php.net/file_exists
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
OR
Source : https://www.php.net/manual/en/function.is-file.php
<?php
var_dump(is_file('a_file.txt')) . "\n";
var_dump(is_file('/usr/bin/')) . "\n";
?>
Upvotes: 2
Reputation: 38092
Check this:
$file = 'Your_file';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
// does not exist
} else {
// exist
}
Upvotes: 1
Reputation: 181
I have same problem and i solve it by giving the actual address ,it ant pretty but it is work for me :
1- first get the addrss for current location and put it in an array:
$pieces = explode("/", __FILE__);
2-change the address to fit the file you want :
array_pop($pieces);
3-finally put it together :
$final = implode("/", $pieces);
4- then put it in file exist function.
Upvotes: 0
Reputation: 5809
<?php
$imgFile = "http://www.technew.in/uploads/image/samsung_galaxy_xpro_2_technew_in.jpg";
if (getimagesize($imgFile)) {
echo "Valid image file";
} else {
echo "Invalid image file or file not exist";
}
?>
Upvotes: 2
Reputation: 3714
It might be that file_exists
does not work properly for files served over the http-protocol. At least it's like that in my case (even with allow_url_fopen
set to On
). I'm assuming the files are coming from a different server where you don't have direct access to the file system. Otherwise it would be easier to just use the full path to the file itself.
You could try it like this though. It's not that nice, but it should at least get the job done. You should be able to find some other examples on the php.net page for file_exists
.
$filename = "http://".$_SERVER['SERVER_NAME']."/media/img/".$row['CatNaam'].".jpg";
echo" <img src=\"".$filename."\" alt=\"".$row['CatNaam']."\">";
echo "filename";
$file_headers = @get_headers($filename);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
echo "The file $filename does not exist";
}
else {
echo "The file $filename exists";
}
Upvotes: 0
Reputation: 1618
This function returns FALSE for files inaccessible due to safe mode restrictions. http://php.net/manual/en/function.file-exists.php
Upvotes: 0
Reputation: 24182
You are checking the file as the client sees it (http...). You need instead to check the actual file on the server's disk, i.e. if you are on linux something like:
if(file_exists($PATH_TO_FILES . '/' . $row['CatNaam'] . ".jpg")) ...
where $PATH_TO_FILES may be computed starting with something like: dirname(__FILE__))
, which will give you the directory your current script sits.
Upvotes: 0
Reputation: 45124
Try the following things
$filename
is properly validatedallow_url_fopen
is activated in your PHP
configUpvotes: 0