file.init.php 4.2 KB

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