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 {
22 * @param $arg mixed ingored argument
24 function WP_Filesystem_Direct($arg) {
25 $this->method = 'direct';
26 $this->errors = new WP_Error();
31 * @return bool Returns true on success or false on failure (always true for WP_Filesystem_Direct).
37 * Reads entire file into a string
39 * @param $file string Name of the file to read.
40 * @return string|bool The function returns the read data or false on failure.
42 function get_contents($file) {
43 return @file_get_contents($file);
46 * Reads entire file into an array
48 * @param $file string Path to the file.
49 * @return array|bool the file contents in an array or false on failure.
51 function get_contents_array($file) {
55 * Write a string to a file
57 * @param $file string Path to the file where to write the data.
58 * @param $contents string The data to write.
59 * @param $mode int (optional) The file permissions as octal number, usually 0644.
60 * @param $type string (optional) Specifies additional type of access you require to the file.
61 * @return bool False upon failure.
63 function put_contents($file, $contents, $mode = false, $type = '') {
64 if ( ! ($fp = @fopen($file, 'w' . $type)) )
66 @fwrite($fp, $contents);
68 $this->chmod($file, $mode);
72 * Gets the current working directory
74 * @return string|bool the current working directory on success, or false on failure.
82 * @param $dir string The new current directory.
83 * @return bool Returns true on success or false on failure.
85 function chdir($dir) {
91 * @param $file string Path to the file.
92 * @param $group mixed A group name or number.
93 * @param $recursive bool (optional) If set True changes file group recursivly. Defaults to False.
94 * @return bool Returns true on success or false on failure.
96 function chgrp($file, $group, $recursive = false) {
97 if ( ! $this->exists($file) )
100 return @chgrp($file, $group);
101 if ( ! $this->is_dir($file) )
102 return @chgrp($file, $group);
103 //Is a directory, and we want recursive
104 $file = trailingslashit($file);
105 $filelist = $this->dirlist($file);
106 foreach ($filelist as $filename)
107 $this->chgrp($file . $filename, $group, $recursive);
112 * Changes filesystem permissions
114 * @param $file string Path to the file.
115 * @param $mode int (optional) The permissions as octal number, usually 0644 for files, 0755 for dirs.
116 * @param $recursive bool (optional) If set True changes file group recursivly. Defaults to False.
117 * @return bool Returns true on success or false on failure.
119 function chmod($file, $mode = false, $recursive = false) {
120 if ( ! $this->exists($file) )
124 if ( $this->is_file($file) )
125 $mode = FS_CHMOD_FILE;
126 elseif ( $this->is_dir($file) )
127 $mode = FS_CHMOD_DIR;
133 return @chmod($file, $mode);
134 if ( ! $this->is_dir($file) )
135 return @chmod($file, $mode);
136 //Is a directory, and we want recursive
137 $file = trailingslashit($file);
138 $filelist = $this->dirlist($file);
139 foreach ($filelist as $filename)
140 $this->chmod($file . $filename, $mode, $recursive);
147 * @param $file string Path to the file.
148 * @param $owner mixed A user name or number.
149 * @param $recursive bool (optional) If set True changes file owner recursivly. Defaults to False.
150 * @return bool Returns true on success or false on failure.
152 function chown($file, $owner, $recursive = false) {
153 if ( ! $this->exists($file) )
156 return @chown($file, $owner);
157 if ( ! $this->is_dir($file) )
158 return @chown($file, $owner);
159 //Is a directory, and we want recursive
160 $filelist = $this->dirlist($file);
161 foreach ($filelist as $filename) {
162 $this->chown($file . '/' . $filename, $owner, $recursive);
169 * @param $file string Path to the file.
170 * @return string Username of the user.
172 function owner($file) {
173 $owneruid = @fileowner($file);
176 if ( ! function_exists('posix_getpwuid') )
178 $ownerarray = posix_getpwuid($owneruid);
179 return $ownerarray['name'];
182 * Gets file permissions
184 * FIXME does not handle errors in fileperms()
186 * @param $file string Path to the file.
187 * @return string Mode of the file (last 4 digits).
189 function getchmod($file) {
190 return substr(decoct(@fileperms($file)),3);
192 function group($file) {
193 $gid = @filegroup($file);
196 if ( ! function_exists('posix_getgrgid') )
198 $grouparray = posix_getgrgid($gid);
199 return $grouparray['name'];
202 function copy($source, $destination, $overwrite = false) {
203 if ( ! $overwrite && $this->exists($destination) )
205 return copy($source, $destination);
208 function move($source, $destination, $overwrite = false) {
209 //Possible to use rename()?
210 if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
211 $this->delete($source);
218 function delete($file, $recursive = false) {
219 if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
221 $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
223 if ( $this->is_file($file) )
224 return @unlink($file);
225 if ( ! $recursive && $this->is_dir($file) )
226 return @rmdir($file);
228 //At this point its a folder, and we're in recursive mode
229 $file = trailingslashit($file);
230 $filelist = $this->dirlist($file, true);
233 if ( is_array($filelist) ) //false if no files, So check first.
234 foreach ($filelist as $filename => $fileinfo)
235 if ( ! $this->delete($file . $filename, $recursive) )
238 if ( file_exists($file) && ! @rmdir($file) )
243 function exists($file) {
244 return @file_exists($file);
247 function is_file($file) {
248 return @is_file($file);
251 function is_dir($path) {
252 return @is_dir($path);
255 function is_readable($file) {
256 return @is_readable($file);
259 function is_writable($file) {
260 return @is_writable($file);
263 function atime($file) {
264 return @fileatime($file);
267 function mtime($file) {
268 return @filemtime($file);
270 function size($file) {
271 return @filesize($file);
274 function touch($file, $time = 0, $atime = 0) {
279 return @touch($file, $time, $atime);
282 function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
284 $chmod = FS_CHMOD_DIR;
286 if ( ! @mkdir($path) )
288 $this->chmod($path, $chmod);
290 $this->chown($path, $chown);
292 $this->chgrp($path, $chgrp);
296 function rmdir($path, $recursive = false) {
297 //Currently unused and untested, Use delete() instead.
299 return @rmdir($path);
301 $filelist = $this->dirlist($path);
302 foreach ($filelist as $filename => $det) {
303 if ( '/' == substr($filename, -1, 1) )
304 $this->rmdir($path . '/' . $filename, $recursive);
307 return @rmdir($path);
310 function dirlist($path, $include_hidden = true, $recursive = false) {
311 if ( $this->is_file($path) ) {
312 $limit_file = basename($path);
313 $path = dirname($path);
318 if ( ! $this->is_dir($path) )
327 while (false !== ($entry = $dir->read()) ) {
329 $struc['name'] = $entry;
331 if ( '.' == $struc['name'] || '..' == $struc['name'] )
334 if ( ! $include_hidden && '.' == $struc['name'][0] )
337 if ( $limit_file && $struc['name'] != $limit_file)
340 $struc['perms'] = $this->gethchmod($path.'/'.$entry);
341 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
342 $struc['number'] = false;
343 $struc['owner'] = $this->owner($path.'/'.$entry);
344 $struc['group'] = $this->group($path.'/'.$entry);
345 $struc['size'] = $this->size($path.'/'.$entry);
346 $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
347 $struc['lastmod'] = date('M j',$struc['lastmodunix']);
348 $struc['time'] = date('h:i:s',$struc['lastmodunix']);
349 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
351 if ( 'd' == $struc['type'] ) {
353 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
355 $struc['files'] = array();
358 $ret[ $struc['name'] ] = $struc;