<?PHP $file = "/usr/test.txt"; if (file_exists($file)) echo "file exists"; //file exists $dir = "/usr/"; if (file_exists($dir)) echo "directory exists"; //directory exists ?>
To check a web file exists or not:
<?PHP $url="http://https://convert.idontcarewhatyouthink.net/program/php/fileexists.php"; $headers = @get_headers($url); if(strpos($headers[0],'200')===false) { echo "$url not exists!\n"; } else echo "$url exists\n"; ?>
To get all files in a directory with certain pattern, use
<?PHP $dir = "/usr/"; $arrfiles = glob('*.txt'); var_dump($arrfiles); ?>
<?PHP $file = "/usr/test.txt"; if (is_file($file)) echo "file exists"; //file exists $dir = "/usr/"; if (is_dir($dir)) echo "directory exists"; //directory exists ?>
The following code compares two directories, and retrieve all files that exist in one directory but not in another.
<?PHP function diff2dir($olddir, $newdir) { $files = scandir($olddir); $newfiles = scandir($newdir); $arr = array(); foreach($files as $file) { $targfile = $newdir . $file; if (!file_exists($targfile)) $arr[] = $file; } return $arr; } ?>