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.
55
* @return bool False upon failure.
57
public function put_contents( $file, $contents, $mode = false ) {
58
$fp = @fopen( $file, 'wb' );
62
mbstring_binary_safe_encoding();
64
$data_length = strlen( $contents );
66
$bytes_written = fwrite( $fp, $contents );
68
reset_mbstring_encoding();
72
if ( $data_length !== $bytes_written )
75
$this->chmod( $file, $mode );
81
* Gets the current working directory
83
* @return string|bool the current working directory on success, or false on failure.
85
public function cwd() {
92
* @param string $dir The new current directory.
93
* @return bool Returns true on success or false on failure.
95
public function chdir($dir) {
102
* @param string $file Path to the file.
103
* @param mixed $group A group name or number.
104
* @param bool $recursive (optional) If set True changes file group recursively. Defaults to False.
105
* @return bool Returns true on success or false on failure.
107
public function chgrp($file, $group, $recursive = false) {
108
if ( ! $this->exists($file) )
111
return @chgrp($file, $group);
112
if ( ! $this->is_dir($file) )
113
return @chgrp($file, $group);
114
// Is a directory, and we want recursive
115
$file = trailingslashit($file);
116
$filelist = $this->dirlist($file);
117
foreach ($filelist as $filename)
118
$this->chgrp($file . $filename, $group, $recursive);
124
* Changes filesystem permissions
126
* @param string $file Path to the file.
127
* @param int $mode (optional) The permissions as octal number, usually 0644 for files, 0755 for dirs.
128
* @param bool $recursive (optional) If set True changes file group recursively. Defaults to False.
129
* @return bool Returns true on success or false on failure.
131
public function chmod($file, $mode = false, $recursive = false) {
133
if ( $this->is_file($file) )
134
$mode = FS_CHMOD_FILE;
135
elseif ( $this->is_dir($file) )
136
$mode = FS_CHMOD_DIR;
141
if ( ! $recursive || ! $this->is_dir($file) )
142
return @chmod($file, $mode);
143
// Is a directory, and we want recursive
144
$file = trailingslashit($file);
145
$filelist = $this->dirlist($file);
146
foreach ( (array)$filelist as $filename => $filemeta)
147
$this->chmod($file . $filename, $mode, $recursive);
155
* @param string $file Path to the file.
156
* @param mixed $owner A user name or number.
157
* @param bool $recursive (optional) If set True changes file owner recursively. Defaults to False.
158
* @return bool Returns true on success or false on failure.
160
public function chown($file, $owner, $recursive = false) {
161
if ( ! $this->exists($file) )
164
return @chown($file, $owner);
165
if ( ! $this->is_dir($file) )
166
return @chown($file, $owner);
167
// Is a directory, and we want recursive
168
$filelist = $this->dirlist($file);
169
foreach ($filelist as $filename) {
170
$this->chown($file . '/' . $filename, $owner, $recursive);
178
* @param string $file Path to the file.
179
* @return string|bool Username of the user or false on error.
181
public function owner($file) {
182
$owneruid = @fileowner($file);
185
if ( ! function_exists('posix_getpwuid') )
187
$ownerarray = posix_getpwuid($owneruid);
188
return $ownerarray['name'];
192
* Gets file permissions
194
* FIXME does not handle errors in fileperms()
196
* @param string $file Path to the file.
197
* @return string Mode of the file (last 3 digits).
199
public function getchmod($file) {
200
return substr( decoct( @fileperms( $file ) ), -3 );
203
public function group($file) {
204
$gid = @filegroup($file);
207
if ( ! function_exists('posix_getgrgid') )
209
$grouparray = posix_getgrgid($gid);
210
return $grouparray['name'];
213
public function copy($source, $destination, $overwrite = false, $mode = false) {
214
if ( ! $overwrite && $this->exists($destination) )
217
$rtval = copy($source, $destination);
219
$this->chmod($destination, $mode);
223
public function move($source, $destination, $overwrite = false) {
224
if ( ! $overwrite && $this->exists($destination) )
227
// Try using rename first. if that fails (for example, source is read only) try copy.
228
if ( @rename($source, $destination) )
231
if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
232
$this->delete($source);
239
public function delete($file, $recursive = false, $type = false) {
240
if ( empty( $file ) ) // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
242
$file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise
244
if ( 'f' == $type || $this->is_file($file) )
245
return @unlink($file);
246
if ( ! $recursive && $this->is_dir($file) )
247
return @rmdir($file);
249
// At this point it's a folder, and we're in recursive mode
250
$file = trailingslashit($file);
251
$filelist = $this->dirlist($file, true);
254
if ( is_array( $filelist ) ) {
255
foreach ( $filelist as $filename => $fileinfo ) {
256
if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) )
261
if ( file_exists($file) && ! @rmdir($file) )
267
public function exists($file) {
268
return @file_exists($file);
271
public function is_file($file) {
272
return @is_file($file);
275
public function is_dir($path) {
276
return @is_dir($path);
279
public function is_readable($file) {
280
return @is_readable($file);
283
public function is_writable($file) {
284
return @is_writable($file);
287
public function atime($file) {
288
return @fileatime($file);
291
public function mtime($file) {
292
return @filemtime($file);
295
public function size($file) {
296
return @filesize($file);
299
public function touch($file, $time = 0, $atime = 0) {
304
return @touch($file, $time, $atime);
307
public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
308
// Safe mode fails with a trailing slash under certain PHP versions.
309
$path = untrailingslashit($path);
314
$chmod = FS_CHMOD_DIR;
316
if ( ! @mkdir($path) )
318
$this->chmod($path, $chmod);
320
$this->chown($path, $chown);
322
$this->chgrp($path, $chgrp);
326
public function rmdir($path, $recursive = false) {
327
return $this->delete($path, $recursive);
330
public function dirlist($path, $include_hidden = true, $recursive = false) {
331
if ( $this->is_file($path) ) {
332
$limit_file = basename($path);
333
$path = dirname($path);
338
if ( ! $this->is_dir($path) )
347
while (false !== ($entry = $dir->read()) ) {
349
$struc['name'] = $entry;
351
if ( '.' == $struc['name'] || '..' == $struc['name'] )
354
if ( ! $include_hidden && '.' == $struc['name'][0] )
357
if ( $limit_file && $struc['name'] != $limit_file)
360
$struc['perms'] = $this->gethchmod($path.'/'.$entry);
361
$struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
362
$struc['number'] = false;
363
$struc['owner'] = $this->owner($path.'/'.$entry);
364
$struc['group'] = $this->group($path.'/'.$entry);
365
$struc['size'] = $this->size($path.'/'.$entry);
366
$struc['lastmodunix']= $this->mtime($path.'/'.$entry);
367
$struc['lastmod'] = date('M j',$struc['lastmodunix']);
368
$struc['time'] = date('h:i:s',$struc['lastmodunix']);
369
$struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
371
if ( 'd' == $struc['type'] ) {
373
$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
375
$struc['files'] = array();
378
$ret[ $struc['name'] ] = $struc;