It is often handy to check if the file you are trying to open really exists in the first place before performing any processing on it. Otherwise, the program is likely to raise a runtime exception.
PHP’s built-in library provides some utility functions in this regard. Some of the functions we shall discuss in this chapter are −
-
file_exists() − tests if the file exists
-
is_file() − if the handle returned by the fopen() refers to a file or directory.
-
is_readable() − test if the file you have opened allows reading data
-
is_writable() − test if writing data in the file is allowed
The file_exists() Function
This function works with a file as well as a directory. It checks whether the given file or directory exists or not.
file_exists(string $filename): bool
The only parameter to this function is a string representing the file/directory with full path. The function returns true or false depending upon the file exists or not.
Example
The following program checks if the file “hello.txt” exists or not.
<?php $filename = ''hello.txt if (file_exists($filename)) { $message = "The file $filename exists"; } else { $message = "The file $filename does not exist"; } echo $message; ?>
If the file does exist in the current directory, the message is −
The file hello.txt exists
If not, the message is −
The file hello.txt does not exist
Example
The string pointing to the file may have a relative or absolute path. Assuming that “hello.txt” file is available in a “hello” subdirectory which is inside the current directory.
<?php $filename = ''hello/hello.txt if (file_exists($filename)) { $message = "The file $filename exists"; } else { $message = "The file $filename does not exist"; } echo $message; ?>
It will produce the following output −
The file hello/hello.txt exists
Example
Try giving the absolute path as below −
<?php $filename = ''c:/xampp/htdocs/hello.txt if (file_exists($filename)) { $message = "The file $filename exists"; } else { $message = "The file $filename does not exist"; } echo $message; ?>
It will produce the following output −
The file c:/xampp/htdocs/hello.txt exists
The is_file() Function
The file_exists() function returns true for existing file as well as directory. The is_file() function helps you to determine if it’s a file.
is_file ( string $filename ) : bool
The following example shows how the is_file() function works −
<?php $filename = ''hello.txt if (is_file($filename)) { $message = "$filename is a file"; } else { $message = "$filename is a not a file"; } echo $message; ?>
The output tells that it is a file −
hello.txt is a file
Now, change the “$filename” to a directory, and see the result −
<?php $filename = hello; if (is_file($filename)) { $message = "$filename is a file"; } else { $message = "$filename is a not a file"; } echo $message; ?>
Now you will be told that “hello” is not a file.
Note that The is_file() function accepts a $filename and returns true only if the $filename is a file and exists.
The is_readable() Function
Sometimes, you may want to check before hand if the file can be read from or not. The is_readable() function can ascertain this fact.
is_readable ( string $filename ) : bool
Example
Given below is an example of how the is_readable() function works −
<?php $filename = ''hello.txt if (is_readable($filename)) { $message = "$filename is readable"; } else { $message = "$filename is not readable"; } echo $message; ?>
It will produce the following output −
hello.txt is readable
The is_writable() Function
You can use the is_writable() function to can check if a file exists and if it is possible to perform write operation on the given file.
is_writable ( string $filename ) : bool
Example
The following example shows how the is_writable() function works −
<?php $filename = ''hello.txt if (is_writable($filename)) { $message = "$filename is writable"; } else { $message = "$filename is not writable"; } echo $message; ?>
For a normal archived file, the program tells that it is writable. However, change its property to “read_only” and run the program. You now get −
hello.txt is writable