file_exists() function in PHP

PHPz
Release: 2023-09-14 08:30:02
forward
2061 people have browsed it

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)
Copy after login

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
  • < /li>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!";
}
?>
Copy after login

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

Output

/doc/candidate.txt does not exist!
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template