file_exists(): Checking file or directory

$add="images/119.jpg";
if(file_exists($add)){
echo "Yes file is there ";
}else{
	echo "Sorry no file at $add ";
}
In our script before executing a program we need to check if file exists or not. This file check method by using file_exists function check the presence of file from relative path we specify and then returns TRUE of False based on the presence of the file.

Example 1: Checking for a File in a Subdirectory

<?php
$file_path = 'docs/sample.pdf';
if (file_exists($file_path)) {
    echo "File found: $file_path";
} else {
    echo "File not found.";
}
?>

Example 2: Checking Multiple Files in a Loop

<?php
$files = ['image1.jpg', 'image2.jpg', 'image3.jpg'];
foreach ($files as $file) {
    if (file_exists($file)) {
        echo "$file exists.<br>";
    } else {
        echo "$file does not exist.<br>";
    }
}
?>

Example 3: Using file_exists() for Directories

<?php
$dir = 'uploads';
if (file_exists($dir) && is_dir($dir)) {
    echo "Directory exists.";
} else {
    echo "Directory not found.";
}
?>
These examples show various ways to use `file_exists()` to check for files and directories.

File Deleting all ( or by selection ) files of a directory Directory Functions
PHP
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Anand

    17-08-2010

    It helps me 2 much. Thanks




    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer