14 May 2011

How to check a file exists on server using php

Sometimes we need to check whether a file or directory exists on server or not, well this can be done by a built-in php function "file_exixts". This function takes the path of the file as argument and check on the server for file, if the file exists on server it returns true and if it is not then it returns false. The syntax and example usage of this function is stated below:

Syntax
bool file_exists ( string $filename )
Example usage
<?php
$file1="example.php";
if (file_exists($file1))
        echo $file1." exists on the server";
else
    echo $file1." does not exist on the server";
?>
You can test this example by placing a file named "example.php" in the same directory as the file containing the example code.

Reference
http://php.net/manual/en/function.file-exists.php

For any queries and issues should you have, please feel free to comment and ask.

No comments: