1 class fileInit {
2
3 /**
4 * 创建空文件
5 * @param string $filename 需要创建的文件
6 * @return
7 */
8 public function create_file($filename) {
9 if (file_exists($filename)) return false;
10 $this->create_dir(dirname($filename)); //创建目录
11 return @file_put_contents($filename,'');
12 }
13
14 /**
15 * 写文件
16 * @param string $filename 文件名称
17 * @param string $content 写入文件的内容
18 * @param string $type 类型,1=清空文件内容,写入新内容,2=再内容后街上新内容
19 * @return
20 */
21 public function write_file($filename, $content, $type = 1) {
22 if ($type == 1) {
23 if (file_exists($filename)) $this->del_file($filename); //删除文件
24 $this->create_file($filename);
25 return $this->write_file($filename, $content, 2);
26 } else {
27 if (!is_writable($filename)) return false;
28 $handle = @fopen($filename, 'a');
29 if (!$handle) return false;
30 $result = @fwrite($handle, $content);
31 if (!$result) return false;
32 @fclose($handle);
33 return true;
34 }
35 }
36
37 /**
38 * 拷贝一个新文件
39 * @param string $filename 文件名称
40 * @param string $newfilename 新文件名称
41 * @return
42 */
43 public function copy_file($filename, $newfilename) {
44 if (!file_exists($filename) || !is_writable($filename)) return false;
45 $this->create_dir(dirname($newfilename)); //创建目录
46 return @copy($filename, $newfilename);
47 }
48
49 /**
50 * 移动文件
51 * @param string $filename 文件名称
52 * @param string $newfilename 新文件名称
53 * @return
54 */
55 public function move_file($filename, $newfilename) {
56 if (!file_exists($filename) || !is_writable($filename)) return false;
57 $this->create_dir(dirname($newfilename)); //创建目录
58 return @rename($filename, $newfilename);
59 }
60
61 /**
62 * 删除文件
63 * @param string $filename 文件名称
64 * @return bool
65 */
66 public function del_file($filename) {
67 if (!file_exists($filename) || !is_writable($filename)) return true;
68 return @unlink($filename);
69 }
70
71 /**
72 * 获取文件信息
73 * @param string $filename 文件名称
74 * @return array('上次访问时间','inode 修改时间','取得文件修改时间','大小','类型')
75 */
76 public function get_file_info($filename) {
77 if (!file_exists($filename)) return false;
78 return array(
79 'atime' => date("Y-m-d H:i:s", fileatime($filename)),
80 'ctime' => date("Y-m-d H:i:s", filectime($filename)),
81 'mtime' => date("Y-m-d H:i:s", filemtime($filename)),
82 'size' => filesize($filename),
83 'type' => filetype($filename)
84 );
85 }
86
87 /**
88 * 创建目录
89 * @param string $path 目录
90 * @return
91 */
92 public function create_dir($path) {
93 if (is_dir($path)) return false;
94 fileInit::create_dir(dirname($path));
95 @mkdir($path);
96 @chmod($path, 0777);
97 return true;
98 }
99
100 /**
101 * 删除目录
102 * @param string $path 目录
103 * @return
104 */
105 public function del_dir($path) {
106 $succeed = true;
107 if(file_exists($path)){
108 $objDir = opendir($path);
109 while(false !== ($fileName = readdir($objDir))){
110 if(($fileName != '.') && ($fileName != '..')){
111 chmod("$path/$fileName", 0777);
112 if(!is_dir("$path/$fileName")){
113 if(!@unlink("$path/$fileName")){
114 $succeed = false;
115 break;
116 }
117 }
118 else{
119 self::del_dir("$path/$fileName");
120 }
121 }
122 }
123 if(!readdir($objDir)){
124 @closedir($objDir);
125 if(!@rmdir($path)){
126 $succeed = false;
127 }
128 }
129 }
130 return $succeed;
131 }
132 }
来源:https://www.cnblogs.com/jackoogle/p/4265958.html