- $filename = '/path/to/foo.txt ';
- if (file_exists ($filename)) {
- echo "The file $filename exists";
- } else {
- echo "The file $filename does not exist";
- }
- ?>
Copy CodeIf the file is present, the result of executing the PHP file is: The file C:blablaphphello.txt exists. If the files do not exist, the execution of the PHP file displays the result: the file C:\blabla\phphello.txt does not exist. You can also use the File_exists function to test whether a directory exists, sample code:
- if (file_exists ("C:\blabla\php"))
- {echo "yes";}
- Else
- {echo "No";}
- ?>
Copy CodeInstance
- /**
- * File or directory permission check function
- *
- * @access Public
- * @param string $file _path file path
- * @param bool $rename _PRV Check permission to execute rename () function when checking for modify permissions
- *
- * @return int return value range is {0 <= x <= 15}, the meaning of each value can be introduced by a combination of four-bit binary numbers.
- * return value in binary notation, four bits are represented by high to low respectively
- * Executable Rename () function permission, can append content permission to file, writable file permission, read file permission.
- */
- function File_mode_info ($file _path)
- {
- /* If not present, unreadable, non-writable, non-modified */
- 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 if the directory is readable */
- $dir = @opendir ($file _path);
- if ($dir = = = False)
- {
- return $mark; If the directory fails to open, directly returns the directory is non-modifiable, non-writable, unreadable
- }
- if (@readdir ($dir)!== false)
- {
- $mark ^= 1; Directory readable 001, directory unreadable 000
- }
- @closedir ($dir);
- /* Check if the directory is writable */
- $fp = @fopen ($test _file, ' WB ');
- if ($fp = = = False)
- {
- return $mark; If the file creation in the directory fails, the return is not writable.
- }
- if (@fwrite ($fp, ' Directory Access testing. ')!== false)
- {
- $mark ^= 2; Directory writable readable 011, directory writable unreadable 010
- }
- @fclose ($FP);
- @unlink ($test _file);
- /* Check if 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 the directory for 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 as Read */
- $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 writable readable 111, non-modifiable writable readable 011 ...
- }
- @fclose ($FP);
- /* Check the directory for 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 CodePHP to determine whether a directory exists example:
- /*---------------
- * XML data stream, written to XML file
- * @param $xmlData
- * @return bool|string
- */
- function Writexmlfile ($xmlData)
- {
- $time = time (); Gets the timestamp used to name the file
- $path = DirName (__file__); Gets the current absolute path
- $path = Substr_replace ($path, "", Stripos ($path, "Actions\data")); Replace the inherent path of this file with an empty
- $path. = "Xmlfiles\"; Store Directory Name
- /* Determine if the target directory exists, does not exist, and then new */
- if (!is_dir ($path))
- {
- mkdir ($path); New Catalog
- }
- /* Record full path and file name */
- $filePathAndName = $path. $time. ". XML ";
- /* Open file with file name <时间戳> + <.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 |