file_exists

(PHP 4, PHP 5, PHP 7, PHP 8)

file_existsChecks whether a file or directory exists

Опис

file_exists(string $filename): bool

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

Прогляньте також