file_exists()或is_readable() [英] file_exists() or is_readable()

查看:182
本文介绍了file_exists()或is_readable()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您想要包含PHP文件时,应该使用哪一个?

Which one should you use when you want to include a PHP file?

if(file_exists($file) require "$file";

if(is_readable($file) require "$file";

推荐答案


file_exists() is_readable()

如果要包含PHP文件,应该使用哪一个?

file_exists() or is_readable()
Which one should you use when you want to include a PHP file?

这取决于您是否想知道文件目录是否存在,或者两者是否正常,以及是否需要检查是否可读。

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.

file_exists()

检查文件或目录

如果您明确只想知道文件是否存在,这将返回误报,如果它是目录则返回true。

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.

如果您明确只想知道文件是否可读,则可能会返回误报,如果它是目录,则可能返回true 。

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).

如果你想检查存在并且:

If you want to check exists and:


  1. 只是一个文件:使用 is_file()

  2. 只是一个目录:使用 is_dir()

  3. 是文件还是目录:使用 file_exists()

  1. Is only a file: Use is_file()
  2. Is only a dir: Use is_dir()
  3. Is file or dir: Use file_exists()

如果你想检查存在且可读:

If you want to check exists and is readable:


  1. 并且只是文件:使用 is_file() is_readable()

  2. 并且只有dir:使用 is_dir() is_readable()

  3. 并且是文件或目录:使用 is_readable()

  1. And is only file: Use is_file() and is_readable()
  2. And is only dir: Use is_dir() and is_readable()
  3. And is file or dir: Use is_readable()

您不需要使用 file_exists( ) is_readable(),因为 is_readable()还会检查 file_exists()

使用 is_file()配对 is_readable() code>或 is_dir()仅用于检查文件上是否可以特定,或者具体在一个目录上年。

You don't need to use file_exists() and is_readable() together because is_readable() also checks if file_exists().
Pairing is_readable() with either is_file() or is_dir() is only to check if is readable specifically on a file only or specifically on a directory only.

与大多数PHP和什么是最好的方法/功能/等 - 这取决于场景。

As with most PHP and "what is the best approach/function/etc" - it depends on the scenario.

依赖于 require 来管理系统是否挂起并不是真的有用。想象一下,所需的文件不可用,因此PHP停止,因为要求失败,但文件只是一个文章图像或用户头像 - 你真的不想提供网站导航,徽标,链接,各种页面文本和内容只是因为丢失的文章图片?

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:

如果文件是数据库连接类,允许提供整个页面内容,则文件不存在可能需要系统暂停。

当然,你应该有某种退出策略,例如提供404页面或错误信息 - 抱歉有错,我们会调查它等。

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:

有时文件不存在,或者至少对应用程序的其余部分没有害处。
例如,如果图像文件不存在,您可以提供消息或默认图像,例如用户头像将只有默认头像(如果找不到它们)。

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.

这篇关于file_exists()或is_readable()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆