Home >Backend Development >PHP Tutorial >file_exists() function in PHP

file_exists() function in PHP

PHPz
PHPzforward
2023-09-14 08:29:022102browse

file_exists() function in PHP

The file_exists method checks whether a file or directory exists. It accepts as argument the path of the file or directory to be checked. Here's what it's used for -

  • It's useful when you need to know if a file exists before processing it.

  • In this way, when creating a new file, use this function to know whether the file already exists.

Syntax

file_exists($file_path)

Parameters

  • ##file_path - Set the file or directory to check whether it exists path of. Required.

Return

The file_exists() method returns.

    Returns True if the file or directory exists
  • False if the file or directory does not exist
Example

Let’s See one that checks for the "candidate.txt" file and displays a message even if the file does not exist.

Real-time demonstration

<?php
$myfile = &#39;/doc/candidate.txt&#39;;
if (file_exists($myfile)) {
   echo "$myfile exists!";
} else {
   echo "$myfile does not exist!";
}
?>

The following is the output showing that the file does not exist.

Output

/doc/candidate.txt does not exist!

The above is the detailed content of file_exists() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete