- PHP Tutorial
- PHP - Home
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP Operators
- PHP - Operators
- PHP - Arithmatic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
- PHP Useful Resources
- PHP - Questions & Answers
- PHP - Quick Guide
- PHP - Useful Resources
- PHP - Discussion
PHP - File Existence
It is often handy to check if the file you are trying to open really exists in the first place before performing any processing on it. Otherwise, the program is likely to raise a runtime exception.
PHP’s built-in library provides some utility functions in this regard. Some of the functions we shall discuss in this chapter are −
file_exists() − tests if the file exists
is_file() − if the handle returned by the fopen() refers to a file or directory.
is_readable() − test if the file you have opened allows reading data
is_writable() − test if writing data in the file is allowed
The file_exists() Function
This function works with a file as well as a directory. It checks whether the given file or directory exists or not.
file_exists(string $filename): bool
The only parameter to this function is a string representing the file/directory with full path. The function returns true or false depending upon the file exists or not.
Example
The following program checks if the file "hello.txt" exists or not.
<?php $filename = 'hello.txt'; if (file_exists($filename)) { $message = "The file $filename exists"; } else { $message = "The file $filename does not exist"; } echo $message; ?>
If the file does exist in the current directory, the message is −
The file hello.txt exists
If not, the message is −
The file hello.txt does not exist
Example
The string pointing to the file may have a relative or absolute path. Assuming that "hello.txt" file is available in a "hello" subdirectory which is inside the current directory.
<?php $filename = 'hello/hello.txt'; if (file_exists($filename)) { $message = "The file $filename exists"; } else { $message = "The file $filename does not exist"; } echo $message; ?>
It will produce the following output −
The file hello/hello.txt exists
Example
Try giving the absolute path as below −
<?php $filename = 'c:/xampp/htdocs/hello.txt'; if (file_exists($filename)) { $message = "The file $filename exists"; } else { $message = "The file $filename does not exist"; } echo $message; ?>
It will produce the following output −
The file c:/xampp/htdocs/hello.txt exists
The is_file() Function
The file_exists() function returns true for existing file as well as directory. The is_file() function helps you to determine if it’s a file.
is_file ( string $filename ) : bool
The following example shows how the is_file() function works −
<?php $filename = 'hello.txt'; if (is_file($filename)) { $message = "$filename is a file"; } else { $message = "$filename is a not a file"; } echo $message; ?>
The output tells that it is a file −
hello.txt is a file
Now, change the "$filename" to a directory, and see the result −
<?php $filename = hello; if (is_file($filename)) { $message = "$filename is a file"; } else { $message = "$filename is a not a file"; } echo $message; ?>
Now you will be told that "hello" is not a file.
Note that The is_file() function accepts a $filename and returns true only if the $filename is a file and exists.
The is_readable() Function
Sometimes, you may want to check before hand if the file can be read from or not. The is_readable() function can ascertain this fact.
is_readable ( string $filename ) : bool
Example
Given below is an example of how the is_readable() function works −
<?php $filename = 'hello.txt'; if (is_readable($filename)) { $message = "$filename is readable"; } else { $message = "$filename is not readable"; } echo $message; ?>
It will produce the following output −
hello.txt is readable
The is_writable() Function
You can use the is_writable() function to can check if a file exists and if it is possible to perform write operation on the given file.
is_writable ( string $filename ) : bool
Example
The following example shows how the is_writable() function works −
<?php $filename = 'hello.txt'; if (is_writable($filename)) { $message = "$filename is writable"; } else { $message = "$filename is not writable"; } echo $message; ?>
For a normal archived file, the program tells that it is writable. However, change its property to "read_only" and run the program. You now get −
hello.txt is writable