【问题标题】:PHP clarifation of file_exists function, does it check for the actual file or just a pathfile_exists 函数的 PHP 说明,它是检查实际文件还是仅检查路径
【发布时间】:2014-06-02 08:25:28
【问题描述】:

我有一个 SQL 数据库中的 txt 文件名和路径列表。我有一个只包含几个文件的目录(很多链接都是坏链接)。我正在使用:

$filename = '/path/somename.txt';

if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}

检查文件,但我注意到它总是回来,好像文件在那里一样。对于我正在尝试做的事情,这个功能是否有更好的选择,或者我做错了什么。先感谢您!

【问题讨论】:

    标签: php sql file path file-exists


    【解决方案1】:

    file_exists 应该验证文件是否存在,而不仅仅是文件路径是否存在。

    如果您遇到问题,可以尝试在路径中包含文档根目录。

    【讨论】:

      【解决方案2】:

      试试这个:

      $filename = dirname(__FILE__)."/path/somename.txt";
      
       echo  file_exists($filename) ? "The file $filename exists" : "The file $filename does not exist";
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-03-15
        • 1970-01-01
        • 2017-02-11
        • 2018-12-18
        • 1970-01-01
        • 2011-11-25
        • 1970-01-01
        • 2018-05-07
        相关资源
        最近更新 更多