Which one should you use when yo开发者_JAVA百科u want to include a PHP file?
if(file_exists($file) require "$file";
or
if(is_readable($file) require "$file";
?
file_exists()
oris_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.
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 only a file: Use
is_file()
- Is only a dir: Use
is_dir()
- Is file or dir: Use
file_exists()
If you want to check exists and is readable:
- And is only file: Use
is_file()
andis_readable()
- And is only dir: Use
is_dir()
andis_readable()
- And is file or dir: Use
is_readable()
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.
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.
If your going to require
a file, there's no point in error checking. The point of using require
instead of include
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
.
The answer is: is_readable().
bool is_readable ( string $filename )
Tells whether a file exists and is readable.
http://us3.php.net/manual/en/function.is-readable.php
if (is_file($file) && is_readable($file))
because is_readable($file)
can be true
also for directories.
精彩评论