PHP 的 file_exists() 对我不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1287837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP's file_exists() will not work for me?
提问by JasonDavis
For some reason this PHP code below will not work, I can not figure it out.
出于某种原因,下面的这个 PHP 代码不起作用,我想不通。
It is very strange, file_exists does not seem to see that the image does exist, I have checked to make sure a good file path is being inserted into the file_exists function and it is still acting up
很奇怪,file_exists 似乎没有看到图像确实存在,我已经检查以确保在 file_exists 函数中插入了一个好的文件路径并且它仍然在起作用
If I change file_exists to !file_exists it will return an images that exist and ones that do not exist
如果我将 file_exists 更改为 !file_exists 它将返回存在的图像和不存在的图像
define('SITE_PATH2', 'http://localhost/');
$noimg = SITE_PATH2. 'images/userphoto/noimagesmall.jpg';
$thumb_name = 'http://localhost/images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if (file_exists($thumb_name)) {
$img_name = $thumb_name;
}else{
$img_name = $noimg;
}
echo $img_name;
回答by AvatarKava
file_exists()needs to use a file path on the hard drive, not a URL. So you should have something more like:
file_exists()需要使用硬盘驱动器上的文件路径,而不是 URL。所以你应该有更多的东西:
$thumb_name = $_SERVER['DOCUMENT_ROOT'] . 'images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if(file_exists($thumb_name)) {
some_code
}
回答by SilentGhost
回答by Gumbo
file_existsdoes only work on the local file system.
file_exists只适用于本地文件系统。
So try this if you're using localhost:
因此,如果您使用的是localhost ,请尝试此操作:
$thumb_name = 'images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if (file_exists($_SERVER['DOCUMENT_ROOT'].$thumb_name)) {
$img_name = SITE_PATH2.$thumb_name;
} else {
$img_name = $noimg;
}
回答by Gumbo
Have you enabled the option which allows you to use external URLs? You can set it in php.ini:
您是否启用了允许您使用外部 URL 的选项?你可以在 php.ini 中设置:
allow_url_fopen = 1
回答by StampedeXV
http://php.net/manual/en/function.file-exists.php
http://php.net/manual/en/function.file-exists.php
did you check the comments below?
你检查下面的评论了吗?
Just reading parts of it, but there seem to be several issues.
只是阅读其中的一部分,但似乎有几个问题。
Caching may be a problem. When opening FTP urls it always returns true (they say in the comments) ...
缓存可能是一个问题。打开 FTP 网址时,它总是返回 true(他们在评论中说)...
回答by Mannusanghi
You have to write the file path like "file:///C:/Documents%20and%20Settings/xyz/Desktop/clip_image001.jpg".
你必须像"file:///C:/Documents%20and%20Settings/xyz/Desktop/clip_image001.jpg".
回答by Mannusanghi
Try Below one. Its working for me
试试下面一个。它对我有用
define('SITE_PATH2', 'http://localhost/');
$noimg = SITE_PATH2. 'images/userphoto/noimagesmall.jpg';
$thumb_name = 'http://localhost/images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if ($fileopen = @fopen($thumb_name)) {
$img_name = $thumb_name;
fclose($fileopen);
}else{
$img_name = $noimg;
}
echo $img_name;

