Skip to content
Snippets Groups Projects
Commit 9b33d54e authored by Dries Buytaert's avatar Dries Buytaert
Browse files

- Patch #319183 by drewish: clean up file.inc documentation.

parent 031556da
No related branches found
No related tags found
No related merge requests found
...@@ -14,9 +14,9 @@ ...@@ -14,9 +14,9 @@
* Fields on the file object: * Fields on the file object:
* - fid - File ID * - fid - File ID
* - uid - The {users}.uid of the user who is associated with the file. * - uid - The {users}.uid of the user who is associated with the file.
* - filename - Name of the file with no path components. This may differ * - filename - Name of the file with no path components. This may differ from
* from the basename of the filepath if the file is renamed to avoid * the basename of the filepath if the file is renamed to avoid overwriting
* overwriting an existing file. * an existing file.
* - filepath - Path of the file relative to Drupal root. * - filepath - Path of the file relative to Drupal root.
* - filemime - The file's MIME type. * - filemime - The file's MIME type.
* - filesize - The size of the file in bytes. * - filesize - The size of the file in bytes.
...@@ -108,11 +108,13 @@ function file_create_url($path) { ...@@ -108,11 +108,13 @@ function file_create_url($path) {
* Make sure the destination is a complete path and resides in the file system * Make sure the destination is a complete path and resides in the file system
* directory, if it is not prepend the file system directory. * directory, if it is not prepend the file system directory.
* *
* @param $destination A string containing the path to verify. If this value is * @param $destination
* omitted, Drupal's 'files' directory will be used. * A string containing the path to verify. If this value is omitted, Drupal's
* @return A string containing the path to file, with file system directory * 'files' directory will be used.
* appended if necessary, or FALSE if the path is invalid (i.e. outside the * @return
* configured 'files' or temp directories). * A string containing the path to file, with file system directory appended
* if necessary, or FALSE if the path is invalid (i.e. outside the configured
* 'files' or temp directories).
*/ */
function file_create_path($destination = NULL) { function file_create_path($destination = NULL) {
$file_path = file_directory_path(); $file_path = file_directory_path();
...@@ -138,17 +140,23 @@ function file_create_path($destination = NULL) { ...@@ -138,17 +140,23 @@ function file_create_path($destination = NULL) {
} }
/** /**
* Check that the directory exists and is writable. Directories need to * Check that the directory exists and is writable.
* have execute permissions to be considered a directory by FTP servers, etc. *
* * Directories need to have execute permissions to be considered a directory by
* @param $directory A string containing the name of a directory path. * FTP servers, etc.
* @param $mode A Boolean value to indicate if the directory should be created *
* if it does not exist or made writable if it is read-only. * @param $directory
* @param $form_item An optional string containing the name of a form item that * A string containing the name of a directory path.
* any errors will be attached to. This is useful for settings forms that * @param $mode
* require the user to specify a writable directory. If it can't be made to * A Boolean value to indicate if the directory should be created if it does
* work, a form error will be set preventing them from saving the settings. * not exist or made writable if it is read-only.
* @return FALSE when directory not found, or TRUE when directory exists. * @param $form_item
* An optional string containing the name of a form item that any errors will
* be attached to. This is useful for settings forms that require the user to
* specify a writable directory. If it can't be made to work, a form error
* will be set preventing them from saving the settings.
* @return
* FALSE when directory not found, or TRUE when directory exists.
*/ */
function file_check_directory(&$directory, $mode = FALSE, $form_item = NULL) { function file_check_directory(&$directory, $mode = FALSE, $form_item = NULL) {
$directory = rtrim($directory, '/\\'); $directory = rtrim($directory, '/\\');
...@@ -199,10 +207,11 @@ function file_check_directory(&$directory, $mode = FALSE, $form_item = NULL) { ...@@ -199,10 +207,11 @@ function file_check_directory(&$directory, $mode = FALSE, $form_item = NULL) {
/** /**
* Checks path to see if it is a directory, or a directory/file. * Checks path to see if it is a directory, or a directory/file.
* *
* @param $path A string containing a file path. This will be set to the * @param $path
* directory's path. * A string containing a file path. This will be set to the directory's path.
* @return If the directory is not in a Drupal writable directory, FALSE is * @return
* returned. Otherwise, the base name of the path is returned. * If the directory is not in a Drupal writable directory, FALSE is returned.
* Otherwise, the base name of the path is returned.
*/ */
function file_check_path(&$path) { function file_check_path(&$path) {
// Check if path is a directory. // Check if path is a directory.
...@@ -232,10 +241,13 @@ function file_check_path(&$path) { ...@@ -232,10 +241,13 @@ function file_check_path(&$path) {
* file_check_location('/www/example.com/files/../../../etc/passwd', '/www/example.com/files'); * file_check_location('/www/example.com/files/../../../etc/passwd', '/www/example.com/files');
* @endcode * @endcode
* *
* @param $source A string set to the file to check. * @param $source
* @param $directory A string where the file should be located. * A string set to the file to check.
* @return FALSE if the path does not exist in the directory; * @param $directory
* otherwise, the real path of the source. * A string where the file should be located.
* @return
* FALSE if the path does not exist in the directory; otherwise, the real
* path of the source.
*/ */
function file_check_location($source, $directory = '') { function file_check_location($source, $directory = '') {
$check = realpath($source); $check = realpath($source);
...@@ -315,6 +327,7 @@ function file_load($param, $reset = NULL) { ...@@ -315,6 +327,7 @@ function file_load($param, $reset = NULL) {
* A file object returned by file_load(). * A file object returned by file_load().
* @return * @return
* The updated file object. * The updated file object.
*
* @see hook_file_insert() * @see hook_file_insert()
* @see hook_file_update() * @see hook_file_update()
*/ */
...@@ -365,6 +378,7 @@ function file_save($file) { ...@@ -365,6 +378,7 @@ function file_save($file) {
* - FILE_EXISTS_ERROR - Do nothing and return FALSE. * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
* @return * @return
* File object if the copy is successful, or FALSE in the event of an error. * File object if the copy is successful, or FALSE in the event of an error.
*
* @see file_unmanaged_copy() * @see file_unmanaged_copy()
* @see hook_file_copy() * @see hook_file_copy()
*/ */
...@@ -410,6 +424,7 @@ function file_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) ...@@ -410,6 +424,7 @@ function file_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME)
* - FILE_EXISTS_ERROR - Do nothing and return FALSE. * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
* @return * @return
* The path to the new file, or FALSE in the event of an error. * The path to the new file, or FALSE in the event of an error.
*
* @see file_copy() * @see file_copy()
*/ */
function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) { function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
...@@ -463,13 +478,16 @@ function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXIST ...@@ -463,13 +478,16 @@ function file_unmanaged_copy($source, $destination = NULL, $replace = FILE_EXIST
* Determines the destination path for a file depending on how replacement of * Determines the destination path for a file depending on how replacement of
* existing files should be handled. * existing files should be handled.
* *
* @param $destination A string specifying the desired path. * @param $destination
* @param $replace Replace behavior when the destination file already exists. * A string specifying the desired path.
* @param $replace
* Replace behavior when the destination file already exists.
* - FILE_EXISTS_REPLACE - Replace the existing file. * - FILE_EXISTS_REPLACE - Replace the existing file.
* - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is * - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is
* unique. * unique.
* - FILE_EXISTS_ERROR - Do nothing and return FALSE. * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
* @return The destination file path or FALSE if the file already exists and * @return
* The destination file path or FALSE if the file already exists and
* FILE_EXISTS_ERROR was specified. * FILE_EXISTS_ERROR was specified.
*/ */
function file_destination($destination, $replace) { function file_destination($destination, $replace) {
...@@ -517,6 +535,7 @@ function file_destination($destination, $replace) { ...@@ -517,6 +535,7 @@ function file_destination($destination, $replace) {
* - FILE_EXISTS_ERROR - Do nothing and return FALSE. * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
* @return * @return
* Resulting file object for success, or FALSE in the event of an error. * Resulting file object for success, or FALSE in the event of an error.
*
* @see file_unmanaged_move() * @see file_unmanaged_move()
* @see hook_file_move() * @see hook_file_move()
*/ */
...@@ -554,6 +573,7 @@ function file_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) ...@@ -554,6 +573,7 @@ function file_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME)
* - FILE_EXISTS_ERROR - Do nothing and return FALSE. * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
* @return * @return
* The filepath of the moved file, or FALSE in the event of an error. * The filepath of the moved file, or FALSE in the event of an error.
*
* @see file_move() * @see file_move()
*/ */
function file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) { function file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
...@@ -565,15 +585,18 @@ function file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXIST ...@@ -565,15 +585,18 @@ function file_unmanaged_move($source, $destination = NULL, $replace = FILE_EXIST
} }
/** /**
* Munge the filename as needed for security purposes. For instance the file * Munge the filename as needed for security purposes.
* name "exploit.php.pps" would become "exploit.php_.pps". *
* * For instance the file name "exploit.php.pps" would become "exploit.php_.pps".
* @param $filename The name of a file to modify. *
* @param $extensions A space separated list of extensions that should not * @param $filename
* be altered. * The name of a file to modify.
* @param $alerts Whether alerts (watchdog, drupal_set_message()) should be * @param $extensions
* displayed. * A space separated list of extensions that should not be altered.
* @return $filename The potentially modified $filename. * @param $alerts
* Whether alerts (watchdog, drupal_set_message()) should be displayed.
* @return
* $filename The potentially modified $filename.
*/ */
function file_munge_filename($filename, $extensions, $alerts = TRUE) { function file_munge_filename($filename, $extensions, $alerts = TRUE) {
$original = $filename; $original = $filename;
...@@ -610,20 +633,28 @@ function file_munge_filename($filename, $extensions, $alerts = TRUE) { ...@@ -610,20 +633,28 @@ function file_munge_filename($filename, $extensions, $alerts = TRUE) {
/** /**
* Undo the effect of upload_munge_filename(). * Undo the effect of upload_munge_filename().
* *
* @param $filename string filename * @param $filename
* @return string * String with the filename to be unmunged.
* @return
* An unmunged filename string.
*/ */
function file_unmunge_filename($filename) { function file_unmunge_filename($filename) {
return str_replace('_.', '.', $filename); return str_replace('_.', '.', $filename);
} }
/** /**
* Create a full file path from a directory and filename. If a file with the * Create a full file path from a directory and filename.
* specified name already exists, an alternative will be used. *
* If a file with the specified name already exists, an alternative will be
* used.
* *
* @param $basename string filename * @param $basename
* @param $directory string directory * String filename
* @param $directory
* String directory
* @return * @return
* File path consisting of $directory and a unique filename based off
* of $basename.
*/ */
function file_create_filename($basename, $directory) { function file_create_filename($basename, $directory) {
$destination = $directory . '/' . $basename; $destination = $directory . '/' . $basename;
...@@ -665,6 +696,7 @@ function file_create_filename($basename, $directory) { ...@@ -665,6 +696,7 @@ function file_create_filename($basename, $directory) {
* TRUE for success, FALSE in the event of an error, or an array if the file * TRUE for success, FALSE in the event of an error, or an array if the file
* is being used by another module. The array keys are the module's name and * is being used by another module. The array keys are the module's name and
* the values are the number of references. * the values are the number of references.
*
* @see file_unmanaged_delete() * @see file_unmanaged_delete()
* @see hook_file_references() * @see hook_file_references()
* @see hook_file_delete() * @see hook_file_delete()
...@@ -703,6 +735,7 @@ function file_delete($file, $force = FALSE) { ...@@ -703,6 +735,7 @@ function file_delete($file, $force = FALSE) {
* @return * @return
* TRUE for success or path does not exist, or FALSE in the event of an * TRUE for success or path does not exist, or FALSE in the event of an
* error. * error.
*
* @see file_delete() * @see file_delete()
*/ */
function file_unmanaged_delete($path) { function file_unmanaged_delete($path) {
...@@ -743,8 +776,7 @@ function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) { ...@@ -743,8 +776,7 @@ function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) {
} }
/** /**
* Saves a file upload to a new location. The source file is validated as a * Saves a file upload to a new location.
* proper upload and handled as such.
* *
* The file will be added to the files table as a temporary file. Temporary * The file will be added to the files table as a temporary file. Temporary
* files are periodically cleaned. To make the file permanent file call * files are periodically cleaned. To make the file permanent file call
...@@ -754,11 +786,7 @@ function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) { ...@@ -754,11 +786,7 @@ function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) {
* A string specifying the name of the upload field to save. * A string specifying the name of the upload field to save.
* @param $validators * @param $validators
* An optional, associative array of callback functions used to validate the * An optional, associative array of callback functions used to validate the
* file. The keys are function names and the values arrays of callback * file. See @file_validate for a full discussion of the array format.
* parameters which will be passed in after the user and file objects. The
* functions should return an array of error messages, an empty array
* indicates that the file passed validation. The functions will be called in
* the order specified.
* @param $destination * @param $destination
* A string containing the directory $source should be copied to. If this is * A string containing the directory $source should be copied to. If this is
* not provided or is not writable, the temporary directory will be used. * not provided or is not writable, the temporary directory will be used.
...@@ -896,6 +924,7 @@ function file_save_upload($source, $validators = array(), $destination = FALSE, ...@@ -896,6 +924,7 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
* the order specified. * the order specified.
* @return * @return
* An array contaning validation error messages. * An array contaning validation error messages.
*
* @see hook_file_validate() * @see hook_file_validate()
*/ */
function file_validate(&$file, $validators = array()) { function file_validate(&$file, $validators = array()) {
...@@ -940,6 +969,8 @@ function file_validate_name_length($file) { ...@@ -940,6 +969,8 @@ function file_validate_name_length($file) {
* @return * @return
* An array. If the file extension is not allowed, it will contain an error * An array. If the file extension is not allowed, it will contain an error
* message. * message.
*
* @see hook_file_validate()
*/ */
function file_validate_extensions($file, $extensions) { function file_validate_extensions($file, $extensions) {
global $user; global $user;
...@@ -954,8 +985,9 @@ function file_validate_extensions($file, $extensions) { ...@@ -954,8 +985,9 @@ function file_validate_extensions($file, $extensions) {
} }
/** /**
* Check that the file's size is below certain limits. This check is not * Check that the file's size is below certain limits.
* enforced for the user #1. *
* This check is not enforced for the user #1.
* *
* @param $file * @param $file
* A Drupal file object. * A Drupal file object.
...@@ -968,6 +1000,8 @@ function file_validate_extensions($file, $extensions) { ...@@ -968,6 +1000,8 @@ function file_validate_extensions($file, $extensions) {
* @return * @return
* An array. If the file size exceeds limits, it will contain an error * An array. If the file size exceeds limits, it will contain an error
* message. * message.
*
* @see hook_file_validate()
*/ */
function file_validate_size($file, $file_limit = 0, $user_limit = 0) { function file_validate_size($file, $file_limit = 0, $user_limit = 0) {
global $user; global $user;
...@@ -994,6 +1028,8 @@ function file_validate_size($file, $file_limit = 0, $user_limit = 0) { ...@@ -994,6 +1028,8 @@ function file_validate_size($file, $file_limit = 0, $user_limit = 0) {
* A Drupal file object. * A Drupal file object.
* @return * @return
* An array. If the file is not an image, it will contain an error message. * An array. If the file is not an image, it will contain an error message.
*
* @see hook_file_validate()
*/ */
function file_validate_is_image(&$file) { function file_validate_is_image(&$file) {
$errors = array(); $errors = array();
...@@ -1008,7 +1044,10 @@ function file_validate_is_image(&$file) { ...@@ -1008,7 +1044,10 @@ function file_validate_is_image(&$file) {
/** /**
* If the file is an image verify that its dimensions are within the specified * If the file is an image verify that its dimensions are within the specified
* maximum and minimum dimensions. Non-image files will be ignored. * maximum and minimum dimensions.
*
* Non-image files will be ignored. If a image toolkit is available the image
* will be scalled to fit within the desired maximum dimensions.
* *
* @param $file * @param $file
* A Drupal file object. This function may resize the file affecting its * A Drupal file object. This function may resize the file affecting its
...@@ -1024,6 +1063,8 @@ function file_validate_is_image(&$file) { ...@@ -1024,6 +1063,8 @@ function file_validate_is_image(&$file) {
* @return * @return
* An array. If the file is an image and did not meet the requirements, it * An array. If the file is an image and did not meet the requirements, it
* will contain an error message. * will contain an error message.
*
* @see hook_file_validate()
*/ */
function file_validate_image_resolution(&$file, $maximum_dimensions = 0, $minimum_dimensions = 0) { function file_validate_image_resolution(&$file, $maximum_dimensions = 0, $minimum_dimensions = 0) {
$errors = array(); $errors = array();
...@@ -1078,6 +1119,7 @@ function file_validate_image_resolution(&$file, $maximum_dimensions = 0, $minimu ...@@ -1078,6 +1119,7 @@ function file_validate_image_resolution(&$file, $maximum_dimensions = 0, $minimu
* - FILE_EXISTS_ERROR - Do nothing and return FALSE. * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
* @return * @return
* A file object, or FALSE on error. * A file object, or FALSE on error.
*
* @see file_unmanaged_save_data() * @see file_unmanaged_save_data()
*/ */
function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) { function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
...@@ -1117,6 +1159,7 @@ function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAM ...@@ -1117,6 +1159,7 @@ function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAM
* - FILE_EXISTS_ERROR - Do nothing and return FALSE. * - FILE_EXISTS_ERROR - Do nothing and return FALSE.
* @return * @return
* A string with the path of the resulting file, or FALSE on error. * A string with the path of the resulting file, or FALSE on error.
*
* @see file_save_data() * @see file_save_data()
*/ */
function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) { function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAME) {
...@@ -1145,6 +1188,7 @@ function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EX ...@@ -1145,6 +1188,7 @@ function file_unmanaged_save_data($data, $destination = NULL, $replace = FILE_EX
* @return * @return
* File object if the change is successful, or FALSE in the event of an * File object if the change is successful, or FALSE in the event of an
* error. * error.
*
* @see hook_file_status() * @see hook_file_status()
*/ */
function file_set_status($file, $status = FILE_STATUS_PERMANENT) { function file_set_status($file, $status = FILE_STATUS_PERMANENT) {
...@@ -1200,7 +1244,7 @@ function file_transfer($source, $headers) { ...@@ -1200,7 +1244,7 @@ function file_transfer($source, $headers) {
} }
/** /**
* Menu handler private file transfers. * Menu handler for private file transfers.
* *
* Call modules that implement hook_file_download() to find out if a file is * Call modules that implement hook_file_download() to find out if a file is
* accessible and what headers it should be transferred with. If a module * accessible and what headers it should be transferred with. If a module
...@@ -1236,6 +1280,7 @@ function file_download() { ...@@ -1236,6 +1280,7 @@ function file_download() {
/** /**
* Finds all files that match a given mask in a given directory. * Finds all files that match a given mask in a given directory.
*
* Directories and files beginning with a period are excluded; this * Directories and files beginning with a period are excluded; this
* prevents hidden files and directories (such as SVN working directories) * prevents hidden files and directories (such as SVN working directories)
* from being scanned. * from being scanned.
...@@ -1261,7 +1306,6 @@ function file_download() { ...@@ -1261,7 +1306,6 @@ function file_download() {
* @param $depth * @param $depth
* Current depth of recursion. This parameter is only used internally and * Current depth of recursion. This parameter is only used internally and
* should not be passed. * should not be passed.
*
* @return * @return
* An associative array (keyed on the provided key) of objects with * An associative array (keyed on the provided key) of objects with
* "path", "basename", and "name" members corresponding to the * "path", "basename", and "name" members corresponding to the
...@@ -1304,7 +1348,8 @@ function file_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $ca ...@@ -1304,7 +1348,8 @@ function file_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $ca
/** /**
* Determine the default temporary directory. * Determine the default temporary directory.
* *
* @return A string containing a temp directory. * @return
* A string containing a temp directory.
*/ */
function file_directory_temp() { function file_directory_temp() {
$temporary_directory = variable_get('file_directory_temp', NULL); $temporary_directory = variable_get('file_directory_temp', NULL);
...@@ -1343,7 +1388,8 @@ function file_directory_temp() { ...@@ -1343,7 +1388,8 @@ function file_directory_temp() {
/** /**
* Determine the default 'files' directory. * Determine the default 'files' directory.
* *
* @return A string containing the path to Drupal's 'files' directory. * @return
* A string containing the path to Drupal's 'files' directory.
*/ */
function file_directory_path() { function file_directory_path() {
return variable_get('file_directory_path', conf_path() . '/files'); return variable_get('file_directory_path', conf_path() . '/files');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment