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

Joh*_*lia 16 php

我看了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;
}
Run Code Online (Sandbox Code Playgroud)

我发现很多帖子都与检查文件是否以PHP格式退出有关,但没有任何内容涉及该目录以及如何最好地检查这个.

这种方法可以调用1000次,所以我可以尽可能快地完成它.

dec*_*eze 27

你正在寻找这个is_file功能:

public function exists() {
    return $this->_file !== null && is_file($this->_file);
}
Run Code Online (Sandbox Code Playgroud)