file.init.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. /*********************************************************************************
  4. * InitPHP 3.8.2 国产PHP开发框架 扩展类库-文件操作类
  5. *-------------------------------------------------------------------------------
  6. * 版权所有: CopyRight By initphp.com
  7. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  8. *-------------------------------------------------------------------------------
  9. * Author:zhuli Dtime:2014-11-25
  10. ***********************************************************************************/
  11. class fileInit {
  12. /**
  13. * 创建空文件
  14. * @param string $filename 需要创建的文件
  15. * @return
  16. */
  17. public function create_file($filename) {
  18. if (file_exists($filename)) return false;
  19. $this->create_dir(dirname($filename)); //创建目录
  20. return @file_put_contents($filename,'');
  21. }
  22. /**
  23. * 写文件
  24. * @param string $filename 文件名称
  25. * @param string $content 写入文件的内容
  26. * @param string $type 类型,1=清空文件内容,写入新内容,2=再内容后街上新内容
  27. * @return
  28. */
  29. public function write_file($filename, $content, $type = 1) {
  30. if ($type == 1) {
  31. if (file_exists($filename)) $this->del_file($filename); //删除文件
  32. $this->create_file($filename);
  33. return $this->write_file($filename, $content, 2);
  34. } else {
  35. if (!is_writable($filename)) return false;
  36. $handle = @fopen($filename, 'a');
  37. if (!$handle) return false;
  38. $result = @fwrite($handle, $content);
  39. if (!$result) return false;
  40. @fclose($handle);
  41. return true;
  42. }
  43. }
  44. /**
  45. * 拷贝一个新文件
  46. * @param string $filename 文件名称
  47. * @param string $newfilename 新文件名称
  48. * @return
  49. */
  50. public function copy_file($filename, $newfilename) {
  51. if (!file_exists($filename) || !is_writable($filename)) return false;
  52. $this->create_dir(dirname($newfilename)); //创建目录
  53. return @copy($filename, $newfilename);
  54. }
  55. /**
  56. * 移动文件
  57. * @param string $filename 文件名称
  58. * @param string $newfilename 新文件名称
  59. * @return
  60. */
  61. public function move_file($filename, $newfilename) {
  62. if (!file_exists($filename) || !is_writable($filename)) return false;
  63. $this->create_dir(dirname($newfilename)); //创建目录
  64. return @rename($filename, $newfilename);
  65. }
  66. /**
  67. * 删除文件
  68. * @param string $filename 文件名称
  69. * @return bool
  70. */
  71. public function del_file($filename) {
  72. if (!file_exists($filename) || !is_writable($filename)) return true;
  73. return @unlink($filename);
  74. }
  75. /**
  76. * 获取文件信息
  77. * @param string $filename 文件名称
  78. * @return array('上次访问时间','inode 修改时间','取得文件修改时间','大小','类型')
  79. */
  80. public function get_file_info($filename) {
  81. if (!file_exists($filename)) return false;
  82. return array(
  83. 'atime' => date("Y-m-d H:i:s", fileatime($filename)),
  84. 'ctime' => date("Y-m-d H:i:s", filectime($filename)),
  85. 'mtime' => date("Y-m-d H:i:s", filemtime($filename)),
  86. 'size' => filesize($filename),
  87. 'type' => filetype($filename)
  88. );
  89. }
  90. /**
  91. * 创建目录
  92. * @param string $path 目录
  93. * @return
  94. */
  95. public function create_dir($path) {
  96. if (is_dir($path)) return false;
  97. fileInit::create_dir(dirname($path));
  98. @mkdir($path);
  99. @chmod($path, 0777);
  100. return true;
  101. }
  102. /**
  103. * 删除目录
  104. * @param string $path 目录
  105. * @return
  106. */
  107. public function del_dir($path) {
  108. $succeed = true;
  109. if(file_exists($path)){
  110. $objDir = opendir($path);
  111. while(false !== ($fileName = readdir($objDir))){
  112. if(($fileName != '.') && ($fileName != '..')){
  113. chmod("$path/$fileName", 0777);
  114. if(!is_dir("$path/$fileName")){
  115. if(!@unlink("$path/$fileName")){
  116. $succeed = false;
  117. break;
  118. }
  119. }
  120. else{
  121. self::del_dir("$path/$fileName");
  122. }
  123. }
  124. }
  125. if(!readdir($objDir)){
  126. @closedir($objDir);
  127. if(!@rmdir($path)){
  128. $succeed = false;
  129. }
  130. }
  131. }
  132. return $succeed;
  133. }
  134. }
  135. ?>