【问题标题】:How do I get a file's path in PHP?如何在 PHP 中获取文件的路径?
【发布时间】:2011-08-23 10:10:46
【问题描述】:

我必须使用 file_exists 函数进行检查... 但是,如果我使用类似的东西

if (file_exists('http://horabola.com/imagens/dt_2845.jpg')) {
  //code
}

没用……

我知道并且我确定文件“dt_2845.jpg”存在于文件夹“imagens”中...... 现在,我该如何检查?如何获取服务器的文件路径?

【问题讨论】:

  • 这张图片是在您的服务器上还是在其他网站上?
  • HTTP Stream Wrappers 不支持file_exists 所属的 stat() 系列函数。请参阅Tip box in the manual 和提到的链接。

标签: php string file path


【解决方案1】:

试试:

if (file_exists($_SERVER['DOCUMENT_ROOT'].'/imagens/dt_2845.jpg')) {
  //code
}

祝你好运

【讨论】:

    【解决方案2】:

    url /的服务器文件系统路径存储在$_SERVER['DOCUMENT_ROOT']predefined variable中。

    当前脚本的路径总是存储在__FILE__ 常量中。您可能想使用

     dirname(__FILE__); 
    

    知道当前脚本的文件系统路径。

    你通过调用尝试了什么:

    file_exists('http://horabola.com/imagens/dt_2845.jpg');
    

    通过 HTTP 打开文件,需要使用 fopen() 而不是 file_exists() (感谢 @Gordon 指出这一点),并且您需要在 PHP 服务器上使用所谓的 @987654323 的 url 包装器@。无论如何,使用 HTTP 协议在与运行脚本相同的服务器上打开文件有点性能浪费(使用 HTTP,即网络,而不是硬盘,速度快 10 到 1000 倍)。

    【讨论】:

    • 请在file_exists 和网址的问题下方查看我的评论。
    猜你喜欢
    • 1970-01-01
    • 2015-03-19
    • 2020-11-01
    • 1970-01-01
    • 2013-12-20
    • 1970-01-01
    • 2019-06-30
    • 2014-06-28
    • 2011-04-22
    相关资源
    最近更新 更多