Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

is_file

Checks that a path exists and is a file rather than a folder.

Syntax

is_file(string $filename): bool

Parameters

ParameterTypeDescription
$filenamestringThe path.

Return value

bool`true` if it is a regular file.

Examples

A file and a folder:

<?php
 
$file = sys_get_temp_dir() . '/ref-is-file.txt';
file_put_contents($file, 'x');
 
var_dump(is_file($file));
var_dump(is_file(sys_get_temp_dir()));
var_dump(file_exists(sys_get_temp_dir()));

Output:

bool(true)
bool(false)
bool(true)

Notes

file_exists() returns true for a folder as well. Before reading a file is_file() is safer.

See also

  • file_exists — Checks whether a file or folder exists.
  • is_dir — Checks whether a path is a folder.
  • is_readable — Checks that a file exists and can be read.

Related recipes

In the tutorial

  • Reading and writing files — Working with files in PHP — writing, reading, appending, reading line by line, checking existence and deleting.

Thanks for reading!

Donate to the site — we work hard for you.