$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.
<?php
$file_path = 'docs/sample.pdf';
if (file_exists($file_path)) {
echo "File found: $file_path";
} else {
echo "File not found.";
}
?>
<?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>";
}
}
?>
<?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.
Anand | 17-08-2010 |
It helps me 2 much. Thanks |