Code for PHP to check if a file or directory exists

Source: Internet
Author: User
    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 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:

    1. if (file_exists ("C:\blabla\php"))
    2. {echo "yes";}
    3. Else
    4. {echo "No";}
    5. ?>
Copy Code

Instance

  1. /**
  2. * File or directory permission check function
  3. *
  4. * @access Public
  5. * @param string $file _path file path
  6. * @param bool $rename _PRV Check permission to execute rename () function when checking for modify permissions
  7. *
  8. * @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.
  9. * return value in binary notation, four bits are represented by high to low respectively
  10. * Executable Rename () function permission, can append content permission to file, writable file permission, read file permission.
  11. */
  12. function File_mode_info ($file _path)
  13. {
  14. /* If not present, unreadable, non-writable, non-modified */
  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 if the directory is readable */
  28. $dir = @opendir ($file _path);
  29. if ($dir = = = False)
  30. {
  31. return $mark; If the directory fails to open, directly returns the directory is non-modifiable, non-writable, unreadable
  32. }
  33. if (@readdir ($dir)!== false)
  34. {
  35. $mark ^= 1; Directory readable 001, directory unreadable 000
  36. }
  37. @closedir ($dir);
  38. /* Check if the directory is writable */
  39. $fp = @fopen ($test _file, ' WB ');
  40. if ($fp = = = False)
  41. {
  42. return $mark; If the file creation in the directory fails, the return is not writable.
  43. }
  44. if (@fwrite ($fp, ' Directory Access testing. ')!== false)
  45. {
  46. $mark ^= 2; Directory writable readable 011, directory writable unreadable 010
  47. }
  48. @fclose ($FP);
  49. @unlink ($test _file);
  50. /* Check if 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 the directory for 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 as Read */
  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 writable readable 111, non-modifiable writable readable 011 ...
  83. }
  84. @fclose ($FP);
  85. /* Check the directory for 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

PHP to determine whether a directory exists example:

  1. /*---------------
  2. * XML data stream, written to XML file
  3. * @param $xmlData
  4. * @return bool|string
  5. */
  6. function Writexmlfile ($xmlData)
  7. {
  8. $time = time (); Gets the timestamp used to name the file
  9. $path = DirName (__file__); Gets the current absolute path
  10. $path = Substr_replace ($path, "", Stripos ($path, "Actions\data")); Replace the inherent path of this file with an empty
  11. $path. = "Xmlfiles\"; Store Directory Name
  12. /* Determine if the target directory exists, does not exist, and then new */
  13. if (!is_dir ($path))
  14. {
  15. mkdir ($path); New Catalog
  16. }
  17. /* Record full path and file name */
  18. $filePathAndName = $path. $time. ". XML ";
  19. /* Open file with file name <时间戳> + <.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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.