Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_exists() or is_readable()

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";

?

like image 254
Co za asy... Avatar asked Jul 07 '11 00:07

Co za asy...


People also ask

What is the use of File_exists () function?

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.

How do you check if a file already exists in PHP?

The file_exists() function checks whether a file or directory exists.

How create file if not exist in PHP?

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

Are PHP files readable?

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.

What is the command to check if a file exists?

-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 $?

Can a file be read or not?

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

How to test if a file is readable in Java?

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.

How to check if a file is readable in PHP?

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


2 Answers

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.

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:

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

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.

like image 73
James Avatar answered Oct 12 '22 00:10

James


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.

like image 43
Nahydrin Avatar answered Oct 11 '22 23:10

Nahydrin