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 Syntax: Standard Tags, Echo, and Short Tags

Understanding PHP syntax is like the basics of any language—PHP syntax defines the rules for writing code that a server…

PHP File Inclusion: require, include, require_once, include_once

PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…

How to Install PHP on a Raspberry Pi

You can use your Raspberry Pi as a personal web server or tool that runs in your home. You do…

PHP $_GET: How to Create Dynamic URLs in PHP?

The PHP $_GET— is a tiny part, but strong in the data processing using the URL. There is a site that…

PHP Arrow Functions: Understanding “fn” Syntax

Arrow functions were introduced in PHP 7.4. They offer you a way to write simple operations, such as calculations, filters,…

PHP array_fill_keys: Create Arrays with Specific Keys

The PHP array_fill_keys function helps you to build an array where each key has the same value. What is the…

PHP file_exists: Check File and Directory Existence

Whether you are just trying to keep your app from crashing or making sure your users’ uploads don’t accidentally overwrite…

PHP is_readable: Check File Accessibility

The PHP is_readable helps you validate file readability before any operation like read or include files. What Is PHP is_readable? The is_readable() function…

PHP array_filter: How to Filter Array Values with Examples

You can use array_filter in PHP to remove unwanted data from arrays. It works with a custom callback or default…

PHP filter_list(): List Available Filters

PHP introduces the filter_list() function to give developers a way to check all available filters in the filter extension. This…

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.