PHP file Operations

Source: Internet
Author: User
  1. FileType ()
  2. Get the type of file
  3. Get the file types under Windows
  4. echo filetype ("D:\\lamp\\php5\\php.ini"). "
    ";
  5. Output file representation is a normal document
  6. echo filetype ("d:\\lamp\\php5"). "
    ";
  7. Output dir indicates a directory
  8. Is_file ()
  9. Determines whether the given file name is a normal file
  10. Is_dir ()
  11. Determines whether a given file name is a directory
  12. File_exists ()
  13. Check whether a file or directory exists
  14. FileSize ()
  15. Get the size of the file
  16. Is_readable ()
  17. Determines whether a given file name is readable
  18. Is_writable ()
  19. Determines whether a given file name can be written
  20. Is_executable ()
  21. Determines whether a given file name is executable
  22. Filectime ()
  23. Gets the creation time of the file
  24. Filemtime ()
  25. Get the file modification time
  26. Fileatime ()
  27. Get access time for a file
  28. Stat ()
  29. Get most property values for a file
  30. Clearstatcache ()
  31. Clear the file information that is cached by PHP
  32. /*
  33. * Declare a function to get most of the properties of a file by passing in a file name
  34. **/
  35. function Getfilepro ($fileName) {
  36. if (!file_exists ($fileName)) {
  37. If the supplied file or directory does not exist, exit the function directly
  38. echo "Destination file does not exist". "
    ";
  39. Return
  40. }
  41. if (Is_file ($fileName)) {
  42. Determines whether a given file is a normal file
  43. echo $fileName. " is an ordinary file
    ";
  44. }
  45. if (Is_dir ($fileName)) {
  46. Determines whether a given file is a directory
  47. echo $fileName. " is a directory
    ";
  48. }
  49. echo "File type:". Getfiletype ($fileName). "
    ";
  50. echo "File Size:". GetFileSize (FileSize ($fileName)). "
    ";
  51. if (is_readable ($fileName)) {
  52. Determine if a file is readable
  53. echo "File readable
    ";
  54. }
  55. if (is_writable ($fileName)) {
  56. Determine if the file is writable
  57. echo "file can be written
    ";
  58. }
  59. if (is_executable ($fileName)) {
  60. Determine if a file is executable
  61. echo "File executable";
  62. }
  63. echo "File creation time is:". Date ("Y-m-d h:i:s", Filectime ($fileName)). "
    ";
  64. echo "The last updated time for the file is:". Date ("Y-m-d h:i:s", Filemtime ($fileName)). "
    ";
  65. The last open time for the echo file is:. Date ("Y-m-d h:i:s", Fileatime ($fileName)). "
    ";
  66. }
  67. /*
  68. * Declare a function to get the type of the file by passing in a file name
  69. */
  70. function Getfiletype ($fileName) {
  71. Switch (filetype ($fileName)) {
  72. Case "File":
  73. $type = "Normal file";
  74. Break
  75. Case "DIR":
  76. $type = "Directory file";
  77. Break
  78. Case "BLOKC":
  79. $type = "block device file";
  80. Break
  81. Case "char":
  82. $type = "character device file";
  83. Break
  84. Case "FIFO":
  85. $type = "Named pipe file";
  86. Break
  87. Case "link":
  88. $type = "symbolic link";
  89. Break
  90. Case "UNKONWN":
  91. $type = "Unknown type";
  92. Break
  93. Default
  94. $type = "No type detected";
  95. }
  96. return $type;
  97. }
  98. /*
  99. * Declare a function to convert the file size
  100. */
  101. function GetFileSize ($bytes) {
  102. if ($bytes >= pow (2,40)) {
  103. If the supplied number of bytes is greater than or equal to 2 of the 40 parties, then the condition is established
  104. $return = Round ($bytes/pow (1024,4), 2);
  105. Convert bytes to the same size as T
  106. $suffix = "TB";
  107. }elseif ($bytes >= pow (2,30)) {
  108. $return = Round ($bytes/pow (1024,3), 2);
  109. Converts bytes to the same size as G
  110. $suffix = "GB";
  111. }elseif ($bytes >= pow (2,20)) {
  112. $return = Round ($bytes/pow (1024,2), 2);
  113. Convert bytes to the same size as M
  114. $suffix = "MB";
  115. }elseif ($bytes >= pow (2,10)) {
  116. $return = Round ($bytes/pow (1024,1), 2);
  117. Convert bytes to the same size as K
  118. $suffix = "KB";
  119. }else{
  120. $return = $bytes;
  121. Convert bytes to the same size as B
  122. $suffix = "B";
  123. }
  124. return $return. " ". $suffix;
  125. }
  126. Getfilepro ("D:\\lamp\\php5\\php.ini");
  127. $filepro =stat ("D:/lamp/php5/php.ini");
  128. Returns most of the properties of a file
  129. Print_r ($filepro);
  130. ?>
Copy Code
Php
  • 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.