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 mixed $arg ignored argument
24 public function __construct($arg) {
25 $this->method = 'direct';
26 $this->errors = new WP_Error();
30 * Reads entire file into a string
32 * @param string $file Name of the file to read.
33 * @return string|bool The function returns the read data or false on failure.
35 public function get_contents($file) {
36 return @file_get_contents($file);
40 * Reads entire file into an array
42 * @param string $file Path to the file.
43 * @return array|bool the file contents in an array or false on failure.
45 public function get_contents_array($file) {
50 * Write a string to a file
52 * @param string $file Remote path to the file where to write the data.
53 * @param string $contents The data to write.
54 * @param int $mode Optional. The file permissions as octal number, usually 0644.
56 * @return bool False upon failure, true otherwise.
58 public function put_contents( $file, $contents, $mode = false ) {
59 $fp = @fopen( $file, 'wb' );
63 mbstring_binary_safe_encoding();
65 $data_length = strlen( $contents );
67 $bytes_written = fwrite( $fp, $contents );
69 reset_mbstring_encoding();
73 if ( $data_length !== $bytes_written )
76 $this->chmod( $file, $mode );
82 * Gets the current working directory
84 * @return string|bool the current working directory on success, or false on failure.
86 public function cwd() {
93 * @param string $dir The new current directory.
94 * @return bool Returns true on success or false on failure.
96 public function chdir($dir) {
103 * @param string $file Path to the file.
104 * @param mixed $group A group name or number.
105 * @param bool $recursive Optional. If set True changes file group recursively. Default false.
106 * @return bool Returns true on success or false on failure.
108 public function chgrp($file, $group, $recursive = false) {
109 if ( ! $this->exists($file) )
112 return @chgrp($file, $group);
113 if ( ! $this->is_dir($file) )
114 return @chgrp($file, $group);
115 // Is a directory, and we want recursive
116 $file = trailingslashit($file);
117 $filelist = $this->dirlist($file);
118 foreach ($filelist as $filename)
119 $this->chgrp($file . $filename, $group, $recursive);
125 * Changes filesystem permissions
127 * @param string $file Path to the file.
128 * @param int $mode Optional. The permissions as octal number, usually 0644 for files,
129 * 0755 for dirs. Default false.
130 * @param bool $recursive Optional. If set True changes file group recursively. Default false.
131 * @return bool Returns true on success or false on failure.
133 public function chmod($file, $mode = false, $recursive = false) {
135 if ( $this->is_file($file) )
136 $mode = FS_CHMOD_FILE;
137 elseif ( $this->is_dir($file) )
138 $mode = FS_CHMOD_DIR;
143 if ( ! $recursive || ! $this->is_dir($file) )
144 return @chmod($file, $mode);
145 // Is a directory, and we want recursive
146 $file = trailingslashit($file);
147 $filelist = $this->dirlist($file);
148 foreach ( (array)$filelist as $filename => $filemeta)
149 $this->chmod($file . $filename, $mode, $recursive);
157 * @param string $file Path to the file.
158 * @param mixed $owner A user name or number.
159 * @param bool $recursive Optional. If set True changes file owner recursively.
161 * @return bool Returns true on success or false on failure.
163 public function chown($file, $owner, $recursive = false) {
164 if ( ! $this->exists($file) )
167 return @chown($file, $owner);
168 if ( ! $this->is_dir($file) )
169 return @chown($file, $owner);
170 // Is a directory, and we want recursive
171 $filelist = $this->dirlist($file);
172 foreach ($filelist as $filename) {
173 $this->chown($file . '/' . $filename, $owner, $recursive);
181 * @param string $file Path to the file.
182 * @return string|bool Username of the user or false on error.
184 public function owner($file) {
185 $owneruid = @fileowner($file);
188 if ( ! function_exists('posix_getpwuid') )
190 $ownerarray = posix_getpwuid($owneruid);
191 return $ownerarray['name'];
195 * Gets file permissions
197 * FIXME does not handle errors in fileperms()
199 * @param string $file Path to the file.
200 * @return string Mode of the file (last 3 digits).
202 public function getchmod($file) {
203 return substr( decoct( @fileperms( $file ) ), -3 );
207 * @param string $file
208 * @return string|false
210 public function group($file) {
211 $gid = @filegroup($file);
214 if ( ! function_exists('posix_getgrgid') )
216 $grouparray = posix_getgrgid($gid);
217 return $grouparray['name'];
221 * @param string $source
222 * @param string $destination
223 * @param bool $overwrite
227 public function copy($source, $destination, $overwrite = false, $mode = false) {
228 if ( ! $overwrite && $this->exists($destination) )
231 $rtval = copy($source, $destination);
233 $this->chmod($destination, $mode);
238 * @param string $source
239 * @param string $destination
240 * @param bool $overwrite
243 public function move($source, $destination, $overwrite = false) {
244 if ( ! $overwrite && $this->exists($destination) )
247 // Try using rename first. if that fails (for example, source is read only) try copy.
248 if ( @rename($source, $destination) )
251 if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
252 $this->delete($source);
260 * @param string $file
261 * @param bool $recursive
262 * @param string $type
265 public function delete($file, $recursive = false, $type = false) {
266 if ( empty( $file ) ) // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
268 $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise
270 if ( 'f' == $type || $this->is_file($file) )
271 return @unlink($file);
272 if ( ! $recursive && $this->is_dir($file) )
273 return @rmdir($file);
275 // At this point it's a folder, and we're in recursive mode
276 $file = trailingslashit($file);
277 $filelist = $this->dirlist($file, true);
280 if ( is_array( $filelist ) ) {
281 foreach ( $filelist as $filename => $fileinfo ) {
282 if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) )
287 if ( file_exists($file) && ! @rmdir($file) )
293 * @param string $file
296 public function exists($file) {
297 return @file_exists($file);
300 * @param string $file
303 public function is_file($file) {
304 return @is_file($file);
307 * @param string $path
310 public function is_dir($path) {
311 return @is_dir($path);
315 * @param string $file
318 public function is_readable($file) {
319 return @is_readable($file);
323 * @param string $file
326 public function is_writable($file) {
327 return @is_writable($file);
331 * @param string $file
334 public function atime($file) {
335 return @fileatime($file);
339 * @param string $file
342 public function mtime($file) {
343 return @filemtime($file);
347 * @param string $file
350 public function size($file) {
351 return @filesize($file);
355 * @param string $file
360 public function touch($file, $time = 0, $atime = 0) {
365 return @touch($file, $time, $atime);
369 * @param string $path
370 * @param mixed $chmod
371 * @param mixed $chown
372 * @param mixed $chgrp
375 public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
376 // Safe mode fails with a trailing slash under certain PHP versions.
377 $path = untrailingslashit($path);
382 $chmod = FS_CHMOD_DIR;
384 if ( ! @mkdir($path) )
386 $this->chmod($path, $chmod);
388 $this->chown($path, $chown);
390 $this->chgrp($path, $chgrp);
395 * @param string $path
396 * @param bool $recursive
399 public function rmdir($path, $recursive = false) {
400 return $this->delete($path, $recursive);
404 * @param string $path
405 * @param bool $include_hidden
406 * @param bool $recursive
409 public function dirlist($path, $include_hidden = true, $recursive = false) {
410 if ( $this->is_file($path) ) {
411 $limit_file = basename($path);
412 $path = dirname($path);
417 if ( ! $this->is_dir($path) )
426 while (false !== ($entry = $dir->read()) ) {
428 $struc['name'] = $entry;
430 if ( '.' == $struc['name'] || '..' == $struc['name'] )
433 if ( ! $include_hidden && '.' == $struc['name'][0] )
436 if ( $limit_file && $struc['name'] != $limit_file)
439 $struc['perms'] = $this->gethchmod($path.'/'.$entry);
440 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
441 $struc['number'] = false;
442 $struc['owner'] = $this->owner($path.'/'.$entry);
443 $struc['group'] = $this->group($path.'/'.$entry);
444 $struc['size'] = $this->size($path.'/'.$entry);
445 $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
446 $struc['lastmod'] = date('M j',$struc['lastmodunix']);
447 $struc['time'] = date('h:i:s',$struc['lastmodunix']);
448 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
450 if ( 'd' == $struc['type'] ) {
452 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
454 $struc['files'] = array();
457 $ret[ $struc['name'] ] = $struc;