简体   繁体   English

PHP的file_exists()对我不起作用?

[英]PHP's file_exists() will not work for me?

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;

file_exists() needs to use a file path on the hard drive, not a URL. file_exists()需要使用硬盘驱动器上的文件路径,而不是URL。 So you should have something more like: 因此,您应该拥有更多类似的东西:

$thumb_name = $_SERVER['DOCUMENT_ROOT'] . 'images/userphoto/1/2/2/59874a886a0356abc1_thumb9.jpg';
if(file_exists($thumb_name)) {
    some_code
}

http://us2.php.net/file_exists http://us2.php.net/file_exists

docs say: 文档说:

As of PHP 5.0.0, this function can also be used with some URL wrappers. 从PHP 5.0.0开始,此功能还可以与某些 URL包装器一起使用。 Refer to List of Supported Protocols/Wrappers for a listing of which wrappers support stat() family of functionality. 有关哪些包装器支持stat()系列功能的列表,请参阅支持的协议/包装器列表。

file_exists does only work on the local file system. file_exists仅在本地文件系统上起作用。

So try this if you're using 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;
}

Have you enabled the option which allows you to use external URLs? 您是否启用了允许您使用外部URL的选项? You can set it in php.ini: 您可以在php.ini中进行设置:

allow_url_fopen = 1

http://php.net/manual/en/function.file-exists.php http://php.net/manual/zh/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(在注释中说)...

您必须编写类似于"file:///C:/Documents%20and%20Settings/xyz/Desktop/clip_image001.jpg"的文件路径。

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM