PHP is_executable() Function

What is PHP is_executable() Function?

If you want to know whether a file or directory exist or not and is executable, use PHP is_executable() function. In your web app development, you want to check whether file is executable before you open it. In this case you can use this function.

Syntax:

is_executable(filename)

Parameters:

The Function has 1 parameter which is required-

filename (Required): It is the path to the file or directory whose executability you want to check.

Return Values:

The function returns-

  • TRUE – if the parameter is a file or directory and is executable.
  • FALSE – if the parameter doesn’t exist; hence not executable or the file exists but not executable.

How to use is_executable() in coding?

As the function returns a Boolean value TRUE/FALSE, you can check this by conditional statements i.e. if statement, ternary operator. Choose one that suits your requirement. Depending on the result that the conditional statement returns, your program flow progresses. Look at the code below-

<?php
if (is_executable("file/demo.txt")) {
    echo 'The file is executable';
} else {
    echo 'The file is not executable';
}
?>

Examples:

Example 1: Confirming that the file exists but not executable-

<?php
if(file_exists("file/deny.txt")){
    if (is_executable("file/demo.txt")) {
        echo 'The file is executable';
    } else {
        echo 'The file exists but is not executable.';
    }
}else{
    echo "File doesn't exist.";
}
?>

Output:

The file exists but is not executable.

Explanation:

Here, file_exists() function confirms that that file “deny.txt” exists. And the is_executable() function confirms that the file exists and is not executable.

Example 2: Checking whether an entry is a file and is executable-

<?php
$filename = "file/demo.txt";
if (is_file($filename) && is_executable($filename)) {
    echo "It is a file and it is executable.";
}else{
    echo "It is not a file.";
}
?>

Output:

It is a file and it is executable.

Explanation:

The is_file() function confirms that “demo.txt” is a file and the is_executable() function confirms that the file is executable. Otherwise the output would be “It is not a file.”

Example 3: Checking whether an entry is a directory and is executable-

<?php
$filename = "file/";
if (is_dir($filename) && is_executable($filename)) {
    echo "It is a directory.";
}else{
    echo "It is not a directory.";
}
?>

Output:

It is a directory and it is executable.

Explanation:

The is_dir() function confirms that “file/” is a directory and the is_executable() function confirms that the directory is executable. Otherwise, the output would be “It is not a directory.”

Notes on is_executable() Function:

  • To check whether the file exists or not, you don’t need to use file_exists() with is_executable() function. Because, when is_executable() function returns TRUE, it confirms the file’s existence.
  • But, when the function returns FALSE, it may happen that the file doesn’t exists or the file exists but not executable. So, confirming file’s existence with file_exists() function, if you use is_executable() function, and it returns FALSE, it means that the file is not executable. Check example 1.
  • As the function returns TRUE for both file and directory, you can use is_file() function with this function to ensure that it is a file. Check example 2.
  • Similarly, as the function returns TRUE for both file and directory, you can use is_dir() function with this function to ensure that it is a directory. Check example 3.

Caution:

PHP cache the result of the is_executable() function. So if you call this function multiple times in a script PHP sends you cached result. The problem is if you modify the file, you won’t get the up-to-date result of the file. So, to get the latest result, clear the cache with clearstatcache() function. Check the code below-

<?php
$filename = "file/demy.txt";
var_dump(is_executable($filename));
// Some codes.
var_dump(is_executable($filename));
// Some codes.
clearstatcache();
var_dump(is_executable($filename));
// Some codes.
?>

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP is_executable() Function

is_executable() which is one of the built-in PHP filesystem functions is very useful when you work with files and directories. It confirms you whether an entry is file/directory and then you can do the other related tasks.

Reference:

https://www.php.net/manual/en/function.is-executable.php