* @uses WP_Filesystem_Base Extends class
*/
class WP_Filesystem_Direct extends WP_Filesystem_Base {
- var $errors = null;
+
/**
* constructor
*
$this->method = 'direct';
$this->errors = new WP_Error();
}
- /**
- * connect filesystem.
- *
- * @return bool Returns true on success or false on failure (always true for WP_Filesystem_Direct).
- */
- function connect() {
- return true;
- }
+
/**
* Reads entire file into a string
*
function get_contents($file) {
return @file_get_contents($file);
}
+
/**
* Reads entire file into an array
*
function get_contents_array($file) {
return @file($file);
}
+
/**
* Write a string to a file
*
* @param int $mode (optional) The file permissions as octal number, usually 0644.
* @return bool False upon failure.
*/
- function put_contents($file, $contents, $mode = false ) {
- if ( ! ($fp = @fopen($file, 'w')) )
+ function put_contents( $file, $contents, $mode = false ) {
+ $fp = @fopen( $file, 'wb' );
+ if ( ! $fp )
+ return false;
+
+ mbstring_binary_safe_encoding();
+
+ $data_length = strlen( $contents );
+
+ $bytes_written = fwrite( $fp, $contents );
+
+ reset_mbstring_encoding();
+
+ fclose( $fp );
+
+ if ( $data_length !== $bytes_written )
return false;
- @fwrite($fp, $contents);
- @fclose($fp);
- $this->chmod($file, $mode);
+
+ $this->chmod( $file, $mode );
+
return true;
}
+
/**
* Gets the current working directory
*
function cwd() {
return @getcwd();
}
+
/**
* Change directory
*
function chdir($dir) {
return @chdir($dir);
}
+
/**
* Changes file group
*
return @chgrp($file, $group);
if ( ! $this->is_dir($file) )
return @chgrp($file, $group);
- //Is a directory, and we want recursive
+ // Is a directory, and we want recursive
$file = trailingslashit($file);
$filelist = $this->dirlist($file);
foreach ($filelist as $filename)
return true;
}
+
/**
* Changes filesystem permissions
*
if ( ! $recursive || ! $this->is_dir($file) )
return @chmod($file, $mode);
- //Is a directory, and we want recursive
+ // Is a directory, and we want recursive
$file = trailingslashit($file);
$filelist = $this->dirlist($file);
foreach ( (array)$filelist as $filename => $filemeta)
return true;
}
+
/**
* Changes file owner
*
return @chown($file, $owner);
if ( ! $this->is_dir($file) )
return @chown($file, $owner);
- //Is a directory, and we want recursive
+ // Is a directory, and we want recursive
$filelist = $this->dirlist($file);
foreach ($filelist as $filename) {
$this->chown($file . '/' . $filename, $owner, $recursive);
}
return true;
}
+
/**
* Gets file owner
*
* @param string $file Path to the file.
- * @return string Username of the user.
+ * @return string|bool Username of the user or false on error.
*/
function owner($file) {
$owneruid = @fileowner($file);
$ownerarray = posix_getpwuid($owneruid);
return $ownerarray['name'];
}
+
/**
* Gets file permissions
*
function getchmod($file) {
return substr(decoct(@fileperms($file)),3);
}
+
function group($file) {
$gid = @filegroup($file);
if ( ! $gid )
if ( ! $overwrite && $this->exists($destination) )
return false;
- // try using rename first. if that fails (for example, source is read only) try copy
+ // try using rename first. if that fails (for example, source is read only) try copy
if ( @rename($source, $destination) )
return true;
}
function delete($file, $recursive = false, $type = false) {
- if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
+ if ( empty( $file ) ) // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
return false;
- $file = str_replace('\\', '/', $file); //for win32, occasional problems deleting files otherwise
+ $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise
if ( 'f' == $type || $this->is_file($file) )
return @unlink($file);
if ( ! $recursive && $this->is_dir($file) )
return @rmdir($file);
- //At this point its a folder, and we're in recursive mode
+ // At this point it's a folder, and we're in recursive mode
$file = trailingslashit($file);
$filelist = $this->dirlist($file, true);
$retval = true;
- if ( is_array($filelist) ) //false if no files, So check first.
- foreach ($filelist as $filename => $fileinfo)
+ if ( is_array( $filelist ) ) {
+ foreach ( $filelist as $filename => $fileinfo ) {
if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) )
$retval = false;
+ }
+ }
if ( file_exists($file) && ! @rmdir($file) )
$retval = false;
+
return $retval;
}
function mtime($file) {
return @filemtime($file);
}
+
function size($file) {
return @filesize($file);
}
return $ret;
}
}
-?>