From aafc3e4b11d0a19825c9797f5e41b75dfab821fb Mon Sep 17 00:00:00 2001 From: Ivan Tcholakov Date: Sat, 6 Jun 2009 00:03:51 +0200 Subject: [PATCH] [svn r21272] FS#306 - Introducing more function in the file system API. --- main/inc/lib/filesystem_api.lib.php | 95 ++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/main/inc/lib/filesystem_api.lib.php b/main/inc/lib/filesystem_api.lib.php index 6285f2f638..ac60f856c1 100644 --- a/main/inc/lib/filesystem_api.lib.php +++ b/main/inc/lib/filesystem_api.lib.php @@ -22,18 +22,99 @@ * character set. */ +/** + * This function attempts to change the mode of the specified file. + * @param string $filename Path to the file. + * @param int $mode Access mode, usually given as octal value, examples: 0777, 0770, 0755, 0644. + * @return bool Returns TRUE on success or FALSE on failure. + * @link http://php.net/chmod + */ +function api_chmod($filename, $mode) { + return chmod(api_file_system_encode($filename), $mode); +} + +/** + * Makes a copy of the file $source to $destination. + * @param string $source Path to the source file. + * @param string $destination The destination path. + * @param resource $context A valid context resource created with stream_context_create(). + * @return bool Returns TRUE on success or FALSE on failure. + * Note: If the destination file already exists, it will be overwritten. + * @link http://php.net/copy + */ +function api_copy($source, $destination, $context = null) { + $source = api_is_system_path($source) ? api_file_system_encode($source) : $source; + $destination = api_is_system_path($destination) ? api_file_system_encode($destination) : $destination; + if (func_num_args() <= 2) { + return copy($source, $destination); + } + return copy($source, $destination, $context); +} + +/** + * Checks whether a file or directory exists. + * @param string $path Path to the file or directory. + * @return bool Returns TRUE if the specified file or directory exists, FALSE otherwise. + * @link http://php.net/manual/en/function.file-exists.php + */ +function api_file_exists($path) { + return file_exists(api_is_system_path($path) ? api_file_system_encode($path) : $path); +} + +/** + * Gets the size for the given file. + * @param string $filename Path to the file. + * @return int Returns the size of the file in bytes, or FALSE in case of an error. + * @link http://php.net/manual/en/function.filesize.php + */ +function api_filesize($filename) { + return filesize(api_is_system_path($filename) ? api_file_system_encode($filename) : $filename); +} + +/** + * This function opens file or URL, i.e. it binds a named resource, specified by filename, to a stream. + * @param string $filename The named resource, file or URL. + * @param string $mode Specifies the type of access required to the stream. + * @param bool $use_include_path If '1' or TRUE the file will be searched in the include_path too. + * @param resource $context A valid context resource created with stream_context_create(). + * @return resource Returns a file pointer resource on success, or FALSE on error. + * @link http://php.net/manual/en/function.fopen.php + */ +function api_fopen($filename, $mode, $use_include_path = false, $context = null) { + $filename = api_is_system_path($filename) ? api_file_system_encode($filename) : $filename; + $count = func_num_args(); + if ($count <= 2) { + return fopen($filename, $mode); + } + elseif ($count == 3) { + return fopen($filename, $mode, $use_include_path); + } + return fopen($filename, $mode, $use_include_path, $context); +} + /** * Checks whether a given path represents a directory (folder). - * @param string The path to be checked. + * @param string $path The path to be checked. * @return bool Returns TRUE if we have a directory, FALSE otherwise. + * @link http://php.net/is_dir */ function api_is_dir($path) { return is_dir(api_is_system_path($path) ? api_file_system_encode($path) : $path); } +/** + * Checks whether a given path represents a file. + * @param string $path The path to be checked. + * @return bool Returns TRUE if we have a file, FALSE otherwise. + * @link http://php.net/is_file + */ +function api_is_file($path) { + return is_file(api_is_system_path($path) ? api_file_system_encode($path) : $path); +} + /** * Checks whether a given path represents a system (local) file or folder. - * @param string The path to be checked. + * @param string $path The path to be checked. * @return bool Returns TRUE if we have a system path, FALSE otherwise. */ function api_is_system_path($path) { @@ -51,4 +132,14 @@ function api_mime_content_type($filename) { return mime_content_type(api_file_system_encode($filename)); } +/** + * This function moves an uploaded file to a new location. + * @param string $filename The filename of the uploaded file. + * @param string $destination The destination of the moved file. + * @return bool If the specified file is not a valid upload file or it cannot be moved for some reason, the function will return FALSE. + */ +function api_move_uploaded_file($filename, $destination) { + return move_uploaded_file(api_file_system_encode($filename), api_file_system_encode($destination)); +} + ?>