PHP is_file Function: Check if a File Exists

If you are working with PHP and need a way to confirm if something is indeed a file, the is_file function will be the fit for this task.

In this tutorial, I will walk you through everything you need to know about is_file. You will learn how it works, its syntax, and how to use it.

What is is_file?

The is_file function in PHP is a built-in tool that checks if a given path points to a regular file. This is different from checking if something exists in general because is_file specifically verifies if the path leads to an actual file—not a directory, symbolic link, or any other type of filesystem object.

If You have a directory named “data” and a file named “data.txt” in the same location. If your code assumes “data” refers to a file, it could lead to some frustrating bugs.

Using is_file ensures you are targeting the right object. It is particularly useful when:

  • Validating user-uploaded files.
  • Preventing accidental overwriting of directories.
  • Building scripts that process specific file types.

Anyway, here is the syntax of is_file:

// It returns boolean type
is_file(string $filename)

Here is how it works:

  • Parameter: It takes one argument, $filename, which is the path to the file you want to check.
  • Return Value: It returns true if the path points to a valid file and false otherwise.

Here is a comparing table for is_file with other file functions:

FunctionPurposeReturns
is_fileChecks if a path is a file.true/false
file_existsChecks if a file or directory exists.true/false
is_dirChecks if a path is a directory.true/false
is_readableChecks if a file is readable.true/false

In the following section, you will see an example about is_file in PHP. let’s move on.

PHP is_file Example

Here, I have to check if the file already exists before trying to read it.

$filePath = "documents/report.txt";

if (is_file($filePath)) {
    echo "The file exists";
} else {
    echo "This file is not acceptable.";
}

It checks if report.txt is an actual file. If the file is available, it continues the script; otherwise, it handles error and outputs a message.

Let’s summarize it.

Wrapping Up

The is_file is an built-in function in PHP that validates whether a path is pointing to a file.

Thus, when handling uploads, processing or storing data (temporary files), or just managing your projects, is_file gives you another roadblock to write code securely.

Similar Reads

PHP Type Juggling: How it Works Behind the Scenes

PHP type juggling refers to the dynamic system where the type of a variable is determined by its context in…

Master PHP Iterables: Arrays, Objects, Traversable Interface

The first appearance of PHP iterable was in PHP version ( 7.1 ) – Iterables are a powerful feature in…

PHP Type Casting: How to Convert Variables in PHP

Type casting in PHP refers to the process of converting a variable from one data type to another. This is…

PHP Resource Type | How the get_resource_type() Works

In this tutorial, I will explain what does mean the PHP resource and we are going to cover all PHP…

PHP array_all Function: How it Works with Examples

PHP 8.4 released a new built-in function, array_all checks to check if all array values meet a condition. It gives…

PHP Namespace: How to Group Code (With Example)

PHP namespace solves the problem of name conflicts. Different developers may create functions-classes, or constants with the same name. PHP…

PHP substr Function: How to Extract and Manipulate Strings

The substr() function in PHP returns part of a string. You give it a string, a starting point, and optionally…

PHP Callable & Callback: Pass a Function to Another

The callback in PHP lets you pass functions as arguments and reuse code. It helps you to control how tasks…

PHP abs Function: How to Get Absolute Values

The abs() function in PHP gives you a number without its sign — it always returns a positive value. That…

PHP array_find: How to Locate Array Values with Examples

PHP array_find was released in PHP 8.4 to locate a value inside an array and returns the first match. Understand…

Previous Article

PHP is_readable: Check File Accessibility

Next Article

Check If a Row Exists in PDO

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.