file_exists() 或 is_read()
当您想要包含 PHP 文件时应该使用哪一个?
if(file_exists($file) require "$file";
或者
if(is_readable($file) require "$file";
?
Which one should you use when you want to include a PHP file?
if(file_exists($file) require "$file";
or
if(is_readable($file) require "$file";
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这取决于您是否想知道是否仅存在一个文件或一个目录,或者两者是否都可以,以及是否需要检查是否可读。
有时你只关心文件和/或目录是否存在,而不需要读取,有时你也需要读取。
选项
file_exists()
检查文件是否存在 或目录 存在。
如果您明确只想知道文件是否存在,则这将返回误报,如果它是目录则返回 true。
is_read()
判断一个文件 或目录存在并且可读。
如果您明确只想知道文件是否可读,则可能会返回误报,因为如果它是目录,则可能会返回 true。
您没有提到但可能需要的其他选项:
is_file()
告诉它是否存在并且是一个常规文件(不是目录)。
is_dir()
告诉它是否存在并且是一个 目录(不是文件)。
解决方案&回答
如果您想检查是否存在并且:
is_file()
is_dir()
file_exists()
如果您想检查是否存在且可读:
is_file()
和is_read()
is_dir()
和is_read()
is_read()
您不需要使用
file_exists( )
和is_read()
一起使用,因为is_read()
还会检查file_exists()
是否存在。将
is_read()
与is_file()
或is_dir()
配对只是为了检查是否可读具体仅一个文件或具体仅位于目录中。依赖于 require
与大多数 PHP 一样,“什么是最好的方法/函数/等等”——这取决于场景。
仅依靠
require
来管理系统是否挂起并没有多大用处。想象一下所需的文件不可用,因此 PHP 由于 require 失败而停止,但该文件只是一个文章图像或用户头像 - 您真的不想仅仅因为提供网站导航、徽标、链接、各种页面文本和内容吗?缺少文章图片?重要:
如果该文件是一个数据库连接类,它允许提供整个页面内容,那么该文件不存在可能需要系统停止。
当然,那么您应该有某种退出策略,例如提供 404 页面,或错误消息 - “抱歉出现错误,我们会调查它”等。
非重要:
有时文件不存在是有效的,或者至少不会对应用程序的其余部分造成损害。
例如,如果图像文件不存在,您可以提供消息或默认图像,例如如果找不到用户头像,则用户头像将只有默认头像。
This depends on if you want to know if only a file or a directory exists, or if either is fine, and if you need to check if readable or not.
Sometimes you only care the file and/or directory exist, and don't need to read, sometimes you also need to read.
Options
file_exists()
Checks whether a file or directory exists.
If you explicitly only want to know if a file exists, this will return false positives as will return true if it's a directory.
is_readable()
Tells whether a file or directory exists and is readable.
If you explicitly only want to know if a file is readable, this is likely to return false positives as could return true if it's a directory.
Additional options you've not mentioned, but probably need:
is_file()
Tells whether it exists and is a regular file (not a directory).
is_dir()
Tells whether it exists and is a directory (not a file).
Solution & Answer
If you want to check exists and:
is_file()
is_dir()
file_exists()
If you want to check exists and is readable:
is_file()
andis_readable()
is_dir()
andis_readable()
is_readable()
You don't need to use
file_exists()
andis_readable()
together becauseis_readable()
also checks iffile_exists()
.Pairing
is_readable()
with eitheris_file()
oris_dir()
is only to check if is readable specifically on a file only or specifically on a directory only.Relying on require
As with most PHP and "what is the best approach/function/etc" - it depends on the scenario.
Relying on just
require
to manage if the system hangs or not is not really useful. Imagine the required file is not available, so PHP halts as require fails, but the file is simply an article image or user avatar - do you really not want to serve site navigation, logo, links, all kinds of page text and content just because of a missing article image?VITAL:
If the file is a database connection class, which allows serving of the entire page content, then the file not being there likely requires a system halt.
Of course then you should have some kind of exit strategy, such as serving a 404 page, or an error message - "Sorry there was a fault, we will look into it" etc.
NON VITAL:
It's sometimes valid for the file to not exist, or at least not detrimental to the rest of the application.
Such as if an image file does not exist, you could serve a message or default image, such as a user avatar will just have a default avatar if theirs is not found.
如果您要
require
一个文件,则没有必要进行错误检查。使用require
而不是include
的目的是在文件不存在时停止执行脚本。如果您不关心文件是否存在,只需使用
include
即可。If your going to
require
a file, there's no point in error checking. The point of usingrequire
instead ofinclude
is to hault execution of the script when the file does not exist.If you don't care if the file exists, just use
include
.答案是:is_read()。
https://www.php.net/manual/en/function。是可读的.php
The answer is: is_readable().
https://www.php.net/manual/en/function.is-readable.php
因为对于目录来说
is_read($file)
也可以是true
。because
is_readable($file)
can betrue
also for directories.