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";
?
The file_exists() function in PHP is an inbuilt function which is used to check whether a file or directory exists or not. The path of the file or directory you want to check is passed as a parameter to the file_exists() function which returns True on success and False on failure.
The file_exists() function checks whether a file or directory exists.
PHP Create File - fopen() The fopen() function is also used to create a file. Maybe a little confusing, but in PHP, a file is created using the same function used to open files. If you use fopen() on a file that does not exist, it will create it, given that the file is opened for writing (w) or appending (a).
The is_readable() function in PHP used to check whether the specified file exists and is readable or not. The name of the file is sent as a parameter to the is_readable() function and it returns True if the file exists and is readable.
-a FILE True if file exists. -e FILE True if file exists. -f FILE True if file exists and is a regular file. -r FILE True if file is readable by you. -s FILE True if file exists and is not empty. -w FILE True if the file is writable by you. Show activity on this post. test -r file.txt -a -w file.txt echo $?
Wheter a file can be read or not depends on multiple factors. The following example is simplified, but you should take into account that: someone/something can change access to a file during execution of your applications
isReadable () method of java.nio.file.Files help us to check whether Java virtual machine has appropriate privileges that would allow it to open this file for reading or not. This method test file is readable or not. This method checks that a file exists or not and if file exists then it is readable or not.
Path to the file. Returns true if the file or directory specified by filename exists and is readable, false otherwise. Upon failure, an E_WARNING is emitted. Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody').
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.
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).
If you want to check exists and:
is_file()
is_dir()
file_exists()
If you want to check exists and is readable:
is_file()
and is_readable()
is_dir()
and is_readable()
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With