PHP 检查文件是否存在,而不是目录

php
2022-08-31 00:35:30

我读了也可以返回转弯,如果它指向一个目录。如果只有文件退出,检查的最快方法是什么?file_exists()

目前我有:

/**
 * Check if the file exists.
 *
 * @return bool
 */
public function exists() {
    if(is_null($this->_file)) return false;

    return (!is_dir($this->_file) && file_exists($this->_file)) ? true : false;
}

我发现了很多关于检查PHP中文件是否退出的帖子,但没有谈论目录以及如何最好地检查它。

这种方法可以调用1000s的时间,所以我可以真正做到尽可能快地使用它。


答案 1

您正在寻找is_file函数:

public function exists() {
    return $this->_file !== null && is_file($this->_file);
}

答案 2
public function exists() {
    return !is_dir($this->_file) && file_exists($this->_file);
}

推荐