(PHP 4, PHP 5, PHP 7, PHP 8)
file_exists — Checks whether a file or directory exists
filename
Path to the file or directory.
On windows, use //computername/share/filename or \\computername\share\filename to check files on network shares.
Returns true
if the file or directory specified by
filename
exists; false
otherwise.
Зауваження:
This function will return
false
for symlinks pointing to non-existing files.
Зауваження:
The check is done using the real UID/GID instead of the effective one.
Зауваження: Оскільки цілочисельний тип в PHP є знаковим, а багато платформ використовують 32-бітні цілі числа, деякі функції файлової системи можуть повертати неочікувані результати для файлів, розмір яких перевищує 2 ГБ.
Під час збоїв буде видаватись E_WARNING
.
Приклад #1 Testing whether a file exists
<?php
$filename = '/path/to/foo.txt';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
Зауваження: Рузультати цієї функції кешуються. Докладніше: clearstatcache().
Починаючи з PHP 5.0.0, ця функція також може бути використана з деякими URL-обгортками. На сторінці Підтримувані протоколи та обгортки вказано, які обгортки підтримують сімейство функцій stat().