Wednesday, April 13, 2016

How to check file/directory exist or not in php


To check whether a file is really exist or not in php, we can use function called file_exists(), the same function can also be use to check folder/directory exist or not.

The file_exists() function is for checking whether a file or directory really exist or not, this function take argument/parameter of a string and return boolean value true or false.

file_exists(string $fileName)

Example:

$fileName = '/var/wwww/html/index.html';

if (file_exists($fileName)) {
 
 echo 'the file is exist';

} else {

 echo 'the file is not exist';
 
}

Example:

$directoryName = '/opt/lampp';

if (file_exists($directoryName)) {
 
 echo 'the directory is exist';

} else {

 echo 'the directory is not exist';

}

No comments:

Post a Comment