Home > Backend Development > PHP Tutorial > PHP code to check whether a file or directory exists

PHP code to check whether a file or directory exists

WBOY
Release: 2016-07-25 09:08:04
Original
1206 people have browsed it
  1. $filename = '/path/to/foo.txt';
  2. if (file_exists($filename)) {
  3. echo "The file $filename exists";
  4. } else {
  5. echo "The file $filename does not exist";
  6. }
  7. ?>
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:

  1. if (file_exists("C:blablaphp"))
  2. {echo "yes";}
  3. else
  4. {echo "no";}
  5. ?>
Copy code

Example

  1. /**
  2. * File or directory permission check function
  3. *
  4. * @access public
  5. * @param string $file_path file path
  6. * @param bool $rename_prv Whether to check the permission to execute the rename() function when checking modification permissions
  7. *
  8. * @ 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.
  9. * The return value is in binary notation, and the four digits from high to low respectively represent
  10. * permissions to execute the rename() function, permissions to append content to files, permissions to write files, and permissions to read files.
  11. */
  12. function file_mode_info($file_path)
  13. {
  14. /* If it does not exist, it is unreadable, unwritable, and unchangeable*/
  15. if (! file_exists($file_path))
  16. {
  17. return false;
  18. }
  19. $mark = 0;
  20. if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
  21. {
  22. /* test file*/
  23. $test_file = $file_path . '/cf_test.txt';
  24. /* If it is a directory*/
  25. if (is_dir($file_path))
  26. {
  27. /* Check whether the directory is readable*/
  28. $dir = @opendir($ file_path);
  29. if ($dir === false)
  30. {
  31. return $mark; //If the directory fails to open, return directly to the directory that cannot be modified, written, or read
  32. }
  33. if (@readdir($dir) ! == false)
  34. {
  35. $mark ^= 1; //The directory is readable 001, the directory is unreadable 000
  36. }
  37. @closedir($dir);
  38. /* Check whether the directory is writable*/
  39. $fp = @fopen ($test_file, 'wb');
  40. if ($fp === false)
  41. {
  42. return $mark; //If the file in the directory fails to be created, it returns unwritable.
  43. }
  44. if (@fwrite($fp, 'directory access testing.') !== false)
  45. {
  46. $mark ^= 2; //The directory is writable and readable 011, the directory is writable and unreadable 010
  47. }
  48. @fclose($fp);
  49. @unlink($test_file);
  50. /* Check whether the directory can be modified*/
  51. $fp = @fopen($test_file, 'ab+');
  52. if ($fp === false)
  53. {
  54. return $mark;
  55. }
  56. if (@fwrite($fp, "modify test.rn") !== false)
  57. {
  58. $mark ^= 4;
  59. }
  60. @fclose($fp);
  61. /* Check whether the directory has permission to execute the rename() function*/
  62. if (@rename($test_file, $test_file) !== false)
  63. {
  64. $mark ^= 8;
  65. }
  66. @unlink($test_file );
  67. }
  68. /* If it is a file*/
  69. elseif (is_file($file_path))
  70. {
  71. /* Open in read mode*/
  72. $fp = @fopen($file_path, 'rb');
  73. if ( $fp)
  74. {
  75. $mark ^= 1; //Readable 001
  76. }
  77. @fclose($fp);
  78. /* Try to modify the file*/
  79. $fp = @fopen($file_path, 'ab+') ;
  80. if ($fp && @fwrite($fp, '') !== false)
  81. {
  82. $mark ^= 6; //Can be modified, written and read 111, cannot be modified, written and read 011...
  83. }
  84. @fclose($fp);
  85. /* Check whether the directory has permission to execute the rename() function*/
  86. if (@rename($test_file, $test_file) !== false)
  87. {
  88. $mark ^ = 8;
  89. }
  90. }
  91. }
  92. else
  93. {
  94. if (@is_readable($file_path))
  95. {
  96. $mark ^= 1;
  97. }
  98. if (@is_writable($file_path))
  99. {
  100. $mark ^ = 14;
  101. }
  102. }
  103. return $mark;
  104. }
  105. ?>
Copy code

Example of PHP judging whether the directory exists:

  1. /*---------------
  2. * Write xml data stream to xml file
  3. * @param $xmlData
  4. * @return bool|string
  5. */
  6. function writeXmlFile($xmlData)
  7. {
  8. $time = time(); //Get the timestamp, used to name the file
  9. $path = dirname(__FILE__); //Get the current absolute path
  10. $path = substr_replace($path, "", stripos($path, "actionsdata")); //Replace the inherent path of this file with an empty one
  11. $path .= "xmlFiles"; //Storage directory name
  12. / *Determine whether the target directory exists, if not, create a new one*/
  13. if(!is_dir($path))
  14. {
  15. mkdir($path); //Create a new directory
  16. }
  17. /*Record the complete path and file name*/
  18. $filePathAndName = $path.$time.".xml";
  19. /*Open the file, the file name is + <.xml>*/
  20. $fp = fopen($filePathAndName, "w") ;
  21. if(!$fp)
  22. {
  23. return false;
  24. }
  25. /*Write to file stream*/
  26. $flag = fwrite($fp, $xmlData);
  27. if(!$flag)
  28. {
  29. return false ;
  30. }
  31. fclose($fp);
  32. return $filePathAndName;
  33. }
  34. ?>
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template