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 function __construct($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 string $file 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 string $file 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 string $file Remote path to the file where to write the data.
58 * @param string $contents The data to write.
59 * @param int $mode (optional) The file permissions as octal number, usually 0644.
60 * @return bool False upon failure.
62 function put_contents($file, $contents, $mode = false ) {
63 if ( ! ($fp = @fopen($file, 'w')) )
65 @fwrite($fp, $contents);
67 $this->chmod($file, $mode);
71 * Gets the current working directory
73 * @return string|bool the current working directory on success, or false on failure.
81 * @param string $dir The new current directory.
82 * @return bool Returns true on success or false on failure.
84 function chdir($dir) {
90 * @param string $file Path to the file.
91 * @param mixed $group A group name or number.
92 * @param bool $recursive (optional) If set True changes file group recursively. Defaults to False.
93 * @return bool Returns true on success or false on failure.
95 function chgrp($file, $group, $recursive = false) {
96 if ( ! $this->exists($file) )
99 return @chgrp($file, $group);
100 if ( ! $this->is_dir($file) )
101 return @chgrp($file, $group);
102 //Is a directory, and we want recursive
103 $file = trailingslashit($file);
104 $filelist = $this->dirlist($file);
105 foreach ($filelist as $filename)
106 $this->chgrp($file . $filename, $group, $recursive);
111 * Changes filesystem permissions
113 * @param string $file Path to the file.
114 * @param int $mode (optional) The permissions as octal number, usually 0644 for files, 0755 for dirs.
115 * @param bool $recursive (optional) If set True changes file group recursively. Defaults to False.
116 * @return bool Returns true on success or false on failure.
118 function chmod($file, $mode = false, $recursive = false) {
120 if ( $this->is_file($file) )
121 $mode = FS_CHMOD_FILE;
122 elseif ( $this->is_dir($file) )
123 $mode = FS_CHMOD_DIR;
128 if ( ! $recursive || ! $this->is_dir($file) )
129 return @chmod($file, $mode);
130 //Is a directory, and we want recursive
131 $file = trailingslashit($file);
132 $filelist = $this->dirlist($file);
133 foreach ( (array)$filelist as $filename => $filemeta)
134 $this->chmod($file . $filename, $mode, $recursive);
141 * @param string $file Path to the file.
142 * @param mixed $owner A user name or number.
143 * @param bool $recursive (optional) If set True changes file owner recursively. Defaults to False.
144 * @return bool Returns true on success or false on failure.
146 function chown($file, $owner, $recursive = false) {
147 if ( ! $this->exists($file) )
150 return @chown($file, $owner);
151 if ( ! $this->is_dir($file) )
152 return @chown($file, $owner);
153 //Is a directory, and we want recursive
154 $filelist = $this->dirlist($file);
155 foreach ($filelist as $filename) {
156 $this->chown($file . '/' . $filename, $owner, $recursive);
163 * @param string $file Path to the file.
164 * @return string Username of the user.
166 function owner($file) {
167 $owneruid = @fileowner($file);
170 if ( ! function_exists('posix_getpwuid') )
172 $ownerarray = posix_getpwuid($owneruid);
173 return $ownerarray['name'];
176 * Gets file permissions
178 * FIXME does not handle errors in fileperms()
180 * @param string $file Path to the file.
181 * @return string Mode of the file (last 4 digits).
183 function getchmod($file) {
184 return substr(decoct(@fileperms($file)),3);
186 function group($file) {
187 $gid = @filegroup($file);
190 if ( ! function_exists('posix_getgrgid') )
192 $grouparray = posix_getgrgid($gid);
193 return $grouparray['name'];
196 function copy($source, $destination, $overwrite = false, $mode = false) {
197 if ( ! $overwrite && $this->exists($destination) )
200 $rtval = copy($source, $destination);
202 $this->chmod($destination, $mode);
206 function move($source, $destination, $overwrite = false) {
207 if ( ! $overwrite && $this->exists($destination) )
210 // try using rename first. if that fails (for example, source is read only) try copy
211 if ( @rename($source, $destination) )
214 if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
215 $this->delete($source);
222 function delete($file, $recursive = false, $type = false) {
223 if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
225 $file = str_replace('\\', '/', $file); //for win32, occasional problems deleting files otherwise
227 if ( 'f' == $type || $this->is_file($file) )
228 return @unlink($file);
229 if ( ! $recursive && $this->is_dir($file) )
230 return @rmdir($file);
232 //At this point its a folder, and we're in recursive mode
233 $file = trailingslashit($file);
234 $filelist = $this->dirlist($file, true);
237 if ( is_array($filelist) ) //false if no files, So check first.
238 foreach ($filelist as $filename => $fileinfo)
239 if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) )
242 if ( file_exists($file) && ! @rmdir($file) )
247 function exists($file) {
248 return @file_exists($file);
251 function is_file($file) {
252 return @is_file($file);
255 function is_dir($path) {
256 return @is_dir($path);
259 function is_readable($file) {
260 return @is_readable($file);
263 function is_writable($file) {
264 return @is_writable($file);
267 function atime($file) {
268 return @fileatime($file);
271 function mtime($file) {
272 return @filemtime($file);
274 function size($file) {
275 return @filesize($file);
278 function touch($file, $time = 0, $atime = 0) {
283 return @touch($file, $time, $atime);
286 function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
287 // safe mode fails with a trailing slash under certain PHP versions.
288 $path = untrailingslashit($path);
293 $chmod = FS_CHMOD_DIR;
295 if ( ! @mkdir($path) )
297 $this->chmod($path, $chmod);
299 $this->chown($path, $chown);
301 $this->chgrp($path, $chgrp);
305 function rmdir($path, $recursive = false) {
306 return $this->delete($path, $recursive);
309 function dirlist($path, $include_hidden = true, $recursive = false) {
310 if ( $this->is_file($path) ) {
311 $limit_file = basename($path);
312 $path = dirname($path);
317 if ( ! $this->is_dir($path) )
326 while (false !== ($entry = $dir->read()) ) {
328 $struc['name'] = $entry;
330 if ( '.' == $struc['name'] || '..' == $struc['name'] )
333 if ( ! $include_hidden && '.' == $struc['name'][0] )
336 if ( $limit_file && $struc['name'] != $limit_file)
339 $struc['perms'] = $this->gethchmod($path.'/'.$entry);
340 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
341 $struc['number'] = false;
342 $struc['owner'] = $this->owner($path.'/'.$entry);
343 $struc['group'] = $this->group($path.'/'.$entry);
344 $struc['size'] = $this->size($path.'/'.$entry);
345 $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
346 $struc['lastmod'] = date('M j',$struc['lastmodunix']);
347 $struc['time'] = date('h:i:s',$struc['lastmodunix']);
348 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
350 if ( 'd' == $struc['type'] ) {
352 $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
354 $struc['files'] = array();
357 $ret[ $struc['name'] ] = $struc;