- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP exit()
- PHP exit() vs. break
- PHP isset()
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP mysqli Tutorial
- PHP mysqli Tutorial
- PHP and MySQL Setup
- PHP mysqli: Create Database
- PHP mysqli: Create Table
- PHP mysqli: Insert Record
- PHP mysqli: Update Record
- PHP mysqli: Fetch Record
- PHP mysqli: Delete Record
- PHP mysqli: SignUp Page
- PHP mysqli: LogIn Page
- PHP mysqli: Store User Data
- PHP mysqli Functions
- PHP mysqli_connect()
- PHP mysqli_close()
- PHP mysqli_connect_errno()
- PHP mysqli_connect_error()
- PHP mysqli_query()
- PHP mysqli_fetch_row()
- PHP mysqli_fetch_assoc()
- PHP mysqli_fetch_array()
- PHP mysqli_free_result()
- PHP mysqli_error()
- PHP mysqli_prepare()
- PHP mysqli_stmt_bind_param()
- PHP mysqli_stmt_execute()
- PHP mysqli_stmt_fetch()
- PHP mysqli_stmt_store_result()
- PHP mysqli_stmt_num_rows()
- PHP mysqli_stmt_bind_result()
- PHP mysqli_stmt_get_result()
- PHP mysqli_result class
- PHP mysqli_report()
- PHP error_reporting()
- PHP mysqli_real_escape_string()
- PHP htmlspecialchars()
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP header()
- PHP getallheaders()
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP file_exists() | Check if File or Directory Exists
The PHP file_exists() function is used when we need to check whether a file or a directory exists or not. For example:
<?php $x = file_exists("codescracker.txt"); if($x) echo "<p>The specified file is available.</p>"; else echo "<p>The specified file is not available.</p>"; ?>
The output of the above PHP example on the file_exists() function is:
Since the file codescracker.txt is available in the current directory, the above example produces the output that you have seen.
PHP file_exists() Syntax
The syntax of the file_exists() function in PHP is:
file_exists(path)
PHP file_exists() Example
Consider the following PHP code as an example demonstrating the "file_exists()" function:
<?php $file = "codescracker.txt"; if(file_exists($file)) echo "<p>The file, <b>$file</b> exists.</p>"; else echo "<p>The file, <b>$file</b> does not exists.</p>"; $path = "C:\Users\DEV\codescracker.com"; if(file_exists($path)) echo "<p>The path, <b>$path</b> exists.</p>"; else echo "<p>The path, <b>$path</b> does not exists.</p>"; ?>
In the above program, both the file and the path exist; therefore, the output produced by the above PHP example should be:
In the above example, "codescracker.txt" is assigned to $file. Then file_exists() checks if this file exists. It outputs the file name and message if the file exists. It displays the file name and a message if the file does not exist.
Next, the code sets $path to "C:\Users\DEV\codescracker.com". Then file_exists() checks if this path exists. It outputs the path name and a message if the path exists. It displays the path name and a message if the path does not exist.
When checking directory paths, use the server's file system syntax. This path uses the backslash character (\) to separate directories.
Advantages of the file_exists() function in PHP
- The file exists() function is straightforward and user-friendly. The file or directory path to be checked is the only parameter required, and it returns a boolean value indicating whether the file or directory is present or not.
- For PHP error handling, the file exists() function can be useful. You can prevent errors and guarantee that your code functions properly by verifying that a file or directory is present before attempting to access it.
- When used in conditional statements, the boolean value returned by file exists() enables you to take different actions depending on whether a file or directory exists or not.
Disadvantages of the file_exists() function in PHP
- Only checking for the existence of a file or directory, file exists() has limited functionality. The type of file or directory, its size, and other properties are not disclosed.
- When a file or directory is on a remote server, using the file exists() function to determine whether it exists can occasionally be time-consuming. Your PHP script's performance may suffer as a result.
- Attackers may use your server's file exists() function to check for the existence of sensitive files or directories. To prevent unauthorized access to your server, it is crucial to use the proper access controls and security precautions.
« Previous Tutorial Next Tutorial »