/**
* Write a string to a file
*
- * @param $file string Path to the file where to write the data.
+ * @param $file string Remote path to the file where to write the data.
* @param $contents string The data to write.
* @param $mode int (optional) The file permissions as octal number, usually 0644.
- * @param $type string (optional) Specifies additional type of access you require to the file.
* @return bool False upon failure.
*/
- function put_contents($file, $contents, $mode = false, $type = '') {
- if ( ! ($fp = @fopen($file, 'w' . $type)) )
+ function put_contents($file, $contents, $mode = false ) {
+ if ( ! ($fp = @fopen($file, 'w')) )
return false;
@fwrite($fp, $contents);
@fclose($fp);
* @return bool Returns true on success or false on failure.
*/
function chmod($file, $mode = false, $recursive = false) {
- if ( ! $this->exists($file) )
- return false;
-
if ( ! $mode ) {
if ( $this->is_file($file) )
$mode = FS_CHMOD_FILE;
return false;
}
- if ( ! $recursive )
- return @chmod($file, $mode);
- if ( ! $this->is_dir($file) )
+ if ( ! $recursive || ! $this->is_dir($file) )
return @chmod($file, $mode);
//Is a directory, and we want recursive
$file = trailingslashit($file);
$filelist = $this->dirlist($file);
- foreach ($filelist as $filename)
+ foreach ( (array)$filelist as $filename => $filemeta)
$this->chmod($file . $filename, $mode, $recursive);
return true;
function copy($source, $destination, $overwrite = false) {
if ( ! $overwrite && $this->exists($destination) )
return false;
+
return copy($source, $destination);
}
function move($source, $destination, $overwrite = false) {
- //Possible to use rename()?
+ if ( ! $overwrite && $this->exists($destination) )
+ return false;
+
+ // try using rename first. if that fails (for example, source is read only) try copy
+ if ( @rename($source, $destination) )
+ return true;
+
if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
$this->delete($source);
return true;
}
function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
+ // safe mode fails with a trailing slash under certain PHP versions.
+ $path = untrailingslashit($path);
+ if ( empty($path) )
+ $path = '/';
+
if ( ! $chmod )
$chmod = FS_CHMOD_DIR;
}
function rmdir($path, $recursive = false) {
- //Currently unused and untested, Use delete() instead.
- if ( ! $recursive )
- return @rmdir($path);
- //recursive:
- $filelist = $this->dirlist($path);
- foreach ($filelist as $filename => $det) {
- if ( '/' == substr($filename, -1, 1) )
- $this->rmdir($path . '/' . $filename, $recursive);
- @rmdir($filename);
- }
- return @rmdir($path);
+ return $this->delete($path, $recursive);
}
function dirlist($path, $include_hidden = true, $recursive = false) {