【问题标题】:function file_exists not working in php函数file_exists在php中不起作用
【发布时间】:2012-07-08 16:13:05
【问题描述】:

我一直在尝试查找目录中是否存在 file_exist。如果不是,我想使用不同的图像。但是当我使用 file_exists 函数时,它总是返回 false。

我使用的代码是

while($r=mysql_fetch_row($res))
    {  
        if(!file_exists('http://localhost/dropbox/lib/admin/'.$r[5]))
            {
                $file='http://localhost/dropbox/lib/admin/images/noimage.gif';
            }
        else
            $file='http://localhost/dropbox/lib/admin/'.$r[5];}

但即使文件退出,该函数也总是返回 false。我通过使用检查了这一点

<img src="<?php echo 'http://localhost/dropbox/lib/admin/'.$r[5]; ?>" />

这样可以正确显示图像。

请有人帮助我

【问题讨论】:

  • 你使用 http 地址,但应该使用 /var/www/...

标签: php html


【解决方案1】:

file_exists 使用文件系统路径,而不是 URL。您使用浏览器中的 URL 通过网络浏览器和网络服务器访问您的 PHP 脚本。 PHP脚本本身虽然可以访问本地文件系统并使用它,但它不会通过网络堆栈来访问文件。

所以使用file_exists('C:\\foo\\bar\\dropbox\\lib\\admin\\' ...)之类的东西。

【讨论】:

    【解决方案2】:

    file_exists 不支持使用 HTTP 的地址(您可以看到这一点,因为 stat 不包含在 the list of wrappers supported over HTTP 中)。正如file_exists 文档所说,可以使用一些包装器(例如 FTP)检查远程文件,但无法通过 HTTP 进行检查,因此file_exists 将始终返回 false。

    假设这个文件在本地机器上(这是由localhost 建议的,你需要使用本地文件路径来访问它。很难猜出这对你来说可能是什么,但它可能看起来像/var/www/dropbox....

    【讨论】:

      【解决方案3】:

      file_exists() 检查文件是否存在于本地文件系统中。您正在传递一个 URL。将其更改为您的保管箱目录的本地路径,它应该可以工作:

      if(file_exists('/path/to/your/dropbox'))
      

      【讨论】:

        【解决方案4】:

        file_exists 函数只能用于 PHP 中的 stat() 函数支持的 URL 协议。

        目前,此包装器不支持http 协议。

        http://uk3.php.net/manual/en/wrappers.http.php

        【讨论】:

          【解决方案5】:

          您将 URL 传递给错误参数的 file_exists 函数。而不是在那里传递您的本地路径。

          要了解有关 file_exist() 函数的更多信息,请阅读此 php 手册:

          http://php.net/manual/en/function.file-exists.php

          【讨论】:

            【解决方案6】:

            您将 URL 传递给错误的 file_exists 函数。而不是传递文件夹的本地路径。

            while($r=mysql_fetch_row($res))
            {  
             if(!file_exists('/dropbox/lib/admin/'.$r[5]))
             {
              $file='/dropbox/lib/admin/images/noimage.gif';
             }
             else
             $file='/dropbox/lib/admin/'.$r[5];
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2014-08-05
              • 2017-05-14
              • 2017-03-11
              • 2011-11-05
              • 2010-11-20
              • 2017-02-21
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多