3 * WordPress Direct Filesystem.
6 * @subpackage Filesystem
10 * WordPress Filesystem Class for direct PHP file and folder manipulation.
14 * @subpackage Filesystem
15 * @uses WP_Filesystem_Base Extends class
17 class WP_Filesystem_Direct extends WP_Filesystem_Base {
18 var $permission = null;
20 function WP_Filesystem_Direct($arg) {
21 $this->method = 'direct';
22 $this->errors = new WP_Error();
27 function setDefaultPermissions($perm) {
28 $this->permission = $perm;
30 function get_contents($file) {
31 return @file_get_contents($file);
33 function get_contents_array($file) {
36 function put_contents($file, $contents, $mode = false, $type = '') {
37 if ( ! ($fp = @fopen($file, 'w' . $type)) )
39 @fwrite($fp, $contents);
41 $this->chmod($file,$mode);
47 function chdir($dir) {
50 function chgrp($file, $group, $recursive = false) {
51 if ( ! $this->exists($file) )
54 return @chgrp($file, $group);
55 if ( ! $this->is_dir($file) )
56 return @chgrp($file, $group);
57 //Is a directory, and we want recursive
58 $file = trailingslashit($file);
59 $filelist = $this->dirlist($file);
60 foreach ($filelist as $filename)
61 $this->chgrp($file . $filename, $group, $recursive);
65 function chmod($file, $mode = false, $recursive = false) {
66 if ( ! $this->exists($file) )
70 if ( $this->permission )
71 $mode = $this->permission;
72 elseif ( $this->is_file($file) )
73 $mode = FS_CHMOD_FILE;
74 elseif ( $this->is_dir($file) )
81 return @chmod($file, $mode);
82 if ( ! $this->is_dir($file) )
83 return @chmod($file, $mode);
84 //Is a directory, and we want recursive
85 $file = trailingslashit($file);
86 $filelist = $this->dirlist($file);
87 foreach ($filelist as $filename)
88 $this->chmod($file . $filename, $mode, $recursive);
92 function chown($file, $owner, $recursive = false) {
93 if ( ! $this->exists($file) )
96 return @chown($file, $owner);
97 if ( ! $this->is_dir($file) )
98 return @chown($file, $owner);
99 //Is a directory, and we want recursive
100 $filelist = $this->dirlist($file);
101 foreach ($filelist as $filename){
102 $this->chown($file . '/' . $filename, $owner, $recursive);
106 function owner($file) {
107 $owneruid = @fileowner($file);
110 if ( ! function_exists('posix_getpwuid') )
112 $ownerarray = posix_getpwuid($owneruid);
113 return $ownerarray['name'];
115 function getchmod($file) {
116 return substr(decoct(@fileperms($file)),3);
118 function group($file) {
119 $gid = @filegroup($file);
122 if ( ! function_exists('posix_getgrgid') )
124 $grouparray = posix_getgrgid($gid);
125 return $grouparray['name'];
128 function copy($source, $destination, $overwrite = false) {
129 if ( ! $overwrite && $this->exists($destination) )
131 return copy($source, $destination);
134 function move($source, $destination, $overwrite = false) {
135 //Possible to use rename()?
136 if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ){
137 $this->delete($source);
144 function delete($file, $recursive = false) {
145 if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
147 $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
149 if ( $this->is_file($file) )
150 return @unlink($file);
151 if ( ! $recursive && $this->is_dir($file) )
152 return @rmdir($file);
154 //At this point its a folder, and we're in recursive mode
155 $file = trailingslashit($file);
156 $filelist = $this->dirlist($file, true);
159 if ( is_array($filelist) ) //false if no files, So check first.
160 foreach ($filelist as $filename => $fileinfo)
161 if ( ! $this->delete($file . $filename, $recursive) )
164 if ( file_exists($file) && ! @rmdir($file) )
169 function exists($file) {
170 return @file_exists($file);
173 function is_file($file) {
174 return @is_file($file);
177 function is_dir($path) {
178 return @is_dir($path);
181 function is_readable($file) {
182 return @is_readable($file);
185 function is_writable($file) {
186 return @is_writable($file);
189 function atime($file) {
190 return @fileatime($file);
193 function mtime($file) {
194 return @filemtime($file);
196 function size($file) {
197 return @filesize($file);
200 function touch($file, $time = 0, $atime = 0){
205 return @touch($file, $time, $atime);
208 function mkdir($path, $chmod = false, $chown = false, $chgrp = false){
209 if ( ! @mkdir($path) )
211 $this->chmod($path, $chmod);
213 $this->chown($path, $chown);
215 $this->chgrp($path, $chgrp);
219 function rmdir($path, $recursive = false) {
220 //Currently unused and untested, Use delete() instead.
222 return @rmdir($path);
224 $filelist = $this->dirlist($path);
225 foreach ($filelist as $filename => $det) {
226 if ( '/' == substr($filename, -1, 1) )
227 $this->rmdir($path . '/' . $filename, $recursive);
230 return @rmdir($path);
233 function dirlist($path, $incdot = false, $recursive = false) {
234 if ( $this->is_file($path) ) {
235 $limitFile = basename($path);
236 $path = dirname($path);
240 if ( ! $this->is_dir($path) )
247 while (false !== ($entry = $dir->read()) ) {
249 $struc['name'] = $entry;
251 if ( '.' == $struc['name'] || '..' == $struc['name'] )
252 continue; //Do not care about these folders.
253 if ( '.' == $struc['name'][0] && !$incdot)
255 if ( $limitFile && $struc['name'] != $limitFile)
258 $struc['perms'] = $this->gethchmod($path.'/'.$entry);
259 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
260 $struc['number'] = false;
261 $struc['owner'] = $this->owner($path.'/'.$entry);
262 $struc['group'] = $this->group($path.'/'.$entry);
263 $struc['size'] = $this->size($path.'/'.$entry);
264 $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
265 $struc['lastmod'] = date('M j',$struc['lastmodunix']);
266 $struc['time'] = date('h:i:s',$struc['lastmodunix']);
267 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
269 if ( 'd' == $struc['type'] ) {
271 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
273 $struc['files'] = array();
276 $ret[ $struc['name'] ] = $struc;