is_executable()
The is_executable() function is a built-in PHP function that checks whether a given file is executable. This function returns true if the file is executable and
What Is the is_executable() Function?
is_executable() is a built-in PHP function that tells you whether a file exists and can be run as a program by the current process. It returns true only when both conditions hold; otherwise it returns false.
"Executable" here means the operating system would allow the file to be executed — for example a shell script, a compiled binary, or a .exe on Windows. A regular text file or a .php source file is normally not executable, even though PHP can read it.
This page covers the syntax, what the return value really means, how the check differs across operating systems, and the gotchas (caching, missing files, permission bits) that trip people up.
Syntax
is_executable(string $filename): bool| Parameter | Description |
|---|---|
$filename | Path to the file to check. Can be relative to the script's working directory or an absolute path. |
Return value — true if the file exists and is executable, false otherwise. PHP also emits an E_WARNING if the path is invalid (for example, if a directory in the path can't be traversed).
A Basic Example
The function returns a boolean, so it slots directly into an if condition. Here we point at the PHP binary; on a typical Linux server that path is executable and the first branch runs. The exact path varies by system, which is why the next example creates a file we fully control.
Creating and Testing a File
The check is most meaningful when you control the file's permission bits. The example below writes a tiny shell script, marks it executable with chmod(), and confirms the result:
<?php
$script = sys_get_temp_dir() . '/hello.sh';
file_put_contents($script, "#!/bin/sh\necho hi\n");
// Before chmod: readable but not executable.
var_dump(is_executable($script)); // bool(false)
chmod($script, 0755); // owner rwx, group/other r-x
clearstatcache(); // forget the cached result
var_dump(is_executable($script)); // bool(true)
unlink($script);Two things to notice:
- The file is not executable until the execute permission bit is set, even though it already existed and was readable.
- After changing permissions you should call
clearstatcache()(see below).
See chmod() for how the 0755 octal mode maps onto owner/group/other permissions.
The Stat Cache Gotcha
PHP caches the result of filesystem stat calls (used by is_executable(), is_readable(), is_writable(), file_exists(), and friends) for performance. If you change a file's permissions during the same request and then re-check it, you may get the stale answer:
<?php
$file = sys_get_temp_dir() . '/cache-demo';
touch($file);
is_executable($file); // result is now cached for this path
chmod($file, 0755);
var_dump(is_executable($file)); // may still report the OLD value
clearstatcache();
var_dump(is_executable($file)); // bool(true) — fresh check
unlink($file);Call clearstatcache() after any chmod(), chown(), rename(), or unlink() if you intend to re-test the same path in the same run.
Behavior Across Operating Systems
- Linux / macOS — the result follows the Unix execute bit (
x) for the relevant user/group/other class. A file with mode0644is not executable;0755is. - Windows — there is no execute permission bit. PHP infers "executable" from the file extension: paths ending in
.exe,.bat,.cmd, or.comare treated as executable. Before PHP 7.4,is_executable()always returnedfalseon Windows, so test on your target version.
Because of these differences, never assume a script that reports true on Linux will do the same on Windows, or vice versa.
When Would I Use It?
- Before running an external program with
exec(),shell_exec(), orproc_open()— verify the binary is actually runnable and give a clear error if it isn't, instead of failing deep inside the call. - Deployment / health checks — confirm a helper script (a cron job, a build hook) has the right permissions after being copied or checked out of version control, where the execute bit is sometimes lost.
- Security gating — combined with a fixed path, refuse to run anything that isn't a known, properly-permissioned executable.
Related Functions
is_executable() belongs to a family of permission/type checks — pick the one that matches the question you're asking:
- is_file() — is the path a regular file (not a directory)?
- is_dir() — is the path a directory?
- is_readable() — can the file be read?
- is_writable() — can the file be written?
- file_exists() — does the path exist at all (file or directory)?
- fileperms() — read the raw permission bits.
- chmod() — change a file's permission bits.
Conclusion
is_executable() returns true only when a file both exists and carries execute permission for the current process. Remember three things: the result is OS-dependent (Unix execute bit vs. Windows extension), it is stat-cached so call clearstatcache() after changing permissions, and it's most useful as a guard before running external programs.