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;
19 var $errors = array();
20 function WP_Filesystem_Direct($arg) {
21 $this->method = 'direct';
22 $this->errors = new WP_Error();
23 $this->permission = umask();
28 function setDefaultPermissions($perm) {
29 $this->permission = $perm;
31 function get_contents($file) {
32 return @file_get_contents($file);
34 function get_contents_array($file) {
37 function put_contents($file, $contents, $mode = false, $type = '') {
38 if ( ! ($fp = @fopen($file, 'w' . $type)) )
40 @fwrite($fp, $contents);
42 $this->chmod($file,$mode);
48 function chdir($dir) {
51 function chgrp($file, $group, $recursive = false) {
52 if( ! $this->exists($file) )
55 return @chgrp($file, $group);
56 if( ! $this->is_dir($file) )
57 return @chgrp($file, $group);
58 //Is a directory, and we want recursive
59 $file = trailingslashit($file);
60 $filelist = $this->dirlist($file);
61 foreach($filelist as $filename)
62 $this->chgrp($file . $filename, $group, $recursive);
66 function chmod($file, $mode = false, $recursive = false) {
68 $mode = $this->permission;
69 if( ! $this->exists($file) )
72 return @chmod($file,$mode);
73 if( ! $this->is_dir($file) )
74 return @chmod($file, $mode);
75 //Is a directory, and we want recursive
76 $file = trailingslashit($file);
77 $filelist = $this->dirlist($file);
78 foreach($filelist as $filename)
79 $this->chmod($file . $filename, $mode, $recursive);
83 function chown($file, $owner, $recursive = false) {
84 if( ! $this->exists($file) )
87 return @chown($file, $owner);
88 if( ! $this->is_dir($file) )
89 return @chown($file, $owner);
90 //Is a directory, and we want recursive
91 $filelist = $this->dirlist($file);
92 foreach($filelist as $filename){
93 $this->chown($file . '/' . $filename, $owner, $recursive);
97 function owner($file) {
98 $owneruid = @fileowner($file);
101 if( ! function_exists('posix_getpwuid') )
103 $ownerarray = posix_getpwuid($owneruid);
104 return $ownerarray['name'];
106 function getchmod($file) {
107 return @fileperms($file);
109 function group($file) {
110 $gid = @filegroup($file);
113 if( ! function_exists('posix_getgrgid') )
115 $grouparray = posix_getgrgid($gid);
116 return $grouparray['name'];
119 function copy($source, $destination, $overwrite = false) {
120 if( ! $overwrite && $this->exists($destination) )
122 return copy($source, $destination);
125 function move($source, $destination, $overwrite = false) {
126 //Possible to use rename()?
127 if( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ){
128 $this->delete($source);
135 function delete($file, $recursive = false) {
136 $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
138 if( $this->is_file($file) )
139 return @unlink($file);
140 if( ! $recursive && $this->is_dir($file) )
141 return @rmdir($file);
143 //At this point its a folder, and we're in recursive mode
144 $file = trailingslashit($file);
145 $filelist = $this->dirlist($file, true);
148 if( is_array($filelist) ) //false if no files, So check first.
149 foreach($filelist as $filename => $fileinfo)
150 if( ! $this->delete($file . $filename, $recursive) )
153 if( ! @rmdir($file) )
158 function exists($file) {
159 return @file_exists($file);
162 function is_file($file) {
163 return @is_file($file);
166 function is_dir($path) {
167 return @is_dir($path);
170 function is_readable($file) {
171 return @is_readable($file);
174 function is_writable($file) {
175 return @is_writable($file);
178 function atime($file) {
179 return @fileatime($file);
182 function mtime($file) {
183 return @filemtime($file);
185 function size($file) {
186 return @filesize($file);
189 function touch($file, $time = 0, $atime = 0){
194 return @touch($file, $time, $atime);
197 function mkdir($path, $chmod = false, $chown = false, $chgrp = false){
199 $chmod = $this->permission;
201 if( ! @mkdir($path, $chmod) )
204 $this->chown($path, $chown);
206 $this->chgrp($path, $chgrp);
210 function rmdir($path, $recursive = false) {
211 //Currently unused and untested, Use delete() instead.
213 return @rmdir($path);
215 $filelist = $this->dirlist($path);
216 foreach($filelist as $filename => $det) {
217 if ( '/' == substr($filename, -1, 1) )
218 $this->rmdir($path . '/' . $filename, $recursive);
221 return @rmdir($path);
224 function dirlist($path, $incdot = false, $recursive = false) {
225 if( $this->is_file($path) ) {
226 $limitFile = basename($path);
227 $path = dirname($path);
231 if( ! $this->is_dir($path) )
238 while (false !== ($entry = $dir->read()) ) {
240 $struc['name'] = $entry;
242 if( '.' == $struc['name'] || '..' == $struc['name'] )
243 continue; //Do not care about these folders.
244 if( '.' == $struc['name'][0] && !$incdot)
246 if( $limitFile && $struc['name'] != $limitFile)
249 $struc['perms'] = $this->gethchmod($path.'/'.$entry);
250 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
251 $struc['number'] = false;
252 $struc['owner'] = $this->owner($path.'/'.$entry);
253 $struc['group'] = $this->group($path.'/'.$entry);
254 $struc['size'] = $this->size($path.'/'.$entry);
255 $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
256 $struc['lastmod'] = date('M j',$struc['lastmodunix']);
257 $struc['time'] = date('h:i:s',$struc['lastmodunix']);
258 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
260 if ( 'd' == $struc['type'] ) {
262 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
264 $struc['files'] = array();
267 $ret[ $struc['name'] ] = $struc;