-
- $filename = '/path/to/foo.txt';
- if (file_exists($filename)) {
- echo "The file $filename exists";
- } else {
- echo "The file $filename does not exist";
- }
- ?>
Copy code
If the file exists, the displayed result of executing the PHP file is: The file C:blablaphphello.txt exists.
If the file does not exist, the result of executing the PHP file is: The file C:blablaphphello.txt does not exist.
You can also use the file_exists function to test whether a directory exists, sample code:
-
- if (file_exists("C:blablaphp"))
- {echo "yes";}
- else
- {echo "no";}
- ?>
Copy code
Example
-
- /**
- * File or directory permission check function
- *
- * @access public
- * @param string $file_path file path
- * @param bool $rename_prv Whether to check the permission to execute the rename() function when checking modification permissions
- *
- * @ return int The value range of the return value is {0 <= x <= 15}. The meaning of each value can be derived from the combination of four binary numbers.
- * The return value is in binary notation, and the four digits from high to low respectively represent
- * permissions to execute the rename() function, permissions to append content to files, permissions to write files, and permissions to read files.
- */
- function file_mode_info($file_path)
- {
- /* If it does not exist, it is unreadable, unwritable, and unchangeable*/
- if (! file_exists($file_path))
- {
- return false;
- }
- $mark = 0;
- if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
- {
- /* test file*/
- $test_file = $file_path . '/cf_test.txt';
- /* If it is a directory*/
- if (is_dir($file_path))
- {
- /* Check whether the directory is readable*/
- $dir = @opendir($ file_path);
- if ($dir === false)
- {
- return $mark; //If the directory fails to open, return directly to the directory that cannot be modified, written, or read
- }
- if (@readdir($dir) ! == false)
- {
- $mark ^= 1; //The directory is readable 001, the directory is unreadable 000
- }
- @closedir($dir);
- /* Check whether the directory is writable*/
- $fp = @fopen ($test_file, 'wb');
- if ($fp === false)
- {
- return $mark; //If the file in the directory fails to be created, it returns unwritable.
- }
- if (@fwrite($fp, 'directory access testing.') !== false)
- {
- $mark ^= 2; //The directory is writable and readable 011, the directory is writable and unreadable 010
- }
- @fclose($fp);
- @unlink($test_file);
- /* Check whether the directory can be modified*/
- $fp = @fopen($test_file, 'ab+');
- if ($fp === false)
- {
- return $mark;
- }
- if (@fwrite($fp, "modify test.rn") !== false)
- {
- $mark ^= 4;
- }
- @fclose($fp);
- /* Check whether the directory has permission to execute the rename() function*/
- if (@rename($test_file, $test_file) !== false)
- {
- $mark ^= 8;
- }
- @unlink($test_file );
- }
- /* If it is a file*/
- elseif (is_file($file_path))
- {
- /* Open in read mode*/
- $fp = @fopen($file_path, 'rb');
- if ( $fp)
- {
- $mark ^= 1; //Readable 001
- }
- @fclose($fp);
- /* Try to modify the file*/
- $fp = @fopen($file_path, 'ab+') ;
- if ($fp && @fwrite($fp, '') !== false)
- {
- $mark ^= 6; //Can be modified, written and read 111, cannot be modified, written and read 011...
- }
- @fclose($fp);
- /* Check whether the directory has permission to execute the rename() function*/
- if (@rename($test_file, $test_file) !== false)
- {
- $mark ^ = 8;
- }
- }
- }
- else
- {
- if (@is_readable($file_path))
- {
- $mark ^= 1;
- }
- if (@is_writable($file_path))
- {
- $mark ^ = 14;
- }
- }
- return $mark;
- }
- ?>
Copy code
Example of PHP judging whether the directory exists:
-
- /*---------------
- * Write xml data stream to xml file
- * @param $xmlData
- * @return bool|string
- */
- function writeXmlFile($xmlData)
- {
- $time = time(); //Get the timestamp, used to name the file
- $path = dirname(__FILE__); //Get the current absolute path
- $path = substr_replace($path, "", stripos($path, "actionsdata")); //Replace the inherent path of this file with an empty one
- $path .= "xmlFiles"; //Storage directory name
- / *Determine whether the target directory exists, if not, create a new one*/
- if(!is_dir($path))
- {
- mkdir($path); //Create a new directory
- }
- /*Record the complete path and file name*/
- $filePathAndName = $path.$time.".xml";
- /*Open the file, the file name is + <.xml>*/
- $fp = fopen($filePathAndName, "w") ;
- if(!$fp)
- {
- return false;
- }
- /*Write to file stream*/
- $flag = fwrite($fp, $xmlData);
- if(!$flag)
- {
- return false ;
- }
- fclose($fp);
- return $filePathAndName;
- }
- ?>
Copy code
|