File interface


Enumerations

enum  FILE_DOWNLOADS_PUBLIC
enum  FILE_DOWNLOADS_PRIVATE
enum  FILE_CREATE_DIRECTORY
enum  FILE_MODIFY_PERMISSIONS
enum  FILE_EXISTS_RENAME
enum  FILE_EXISTS_REPLACE
enum  FILE_EXISTS_ERROR
enum  FILE_STATUS_TEMPORARY
enum  FILE_STATUS_PERMANENT

Functions

 file_create_url ($path)
 file_create_path ($dest=0)
 file_check_directory (&$directory, $mode=0, $form_item=NULL)
 file_check_path (&$path)
 file_check_location ($source, $directory= '')
 file_copy (&$source, $dest=0, $replace=FILE_EXISTS_RENAME)
 file_destination ($destination, $replace)
 file_move (&$source, $dest=0, $replace=FILE_EXISTS_RENAME)
 file_munge_filename ($filename, $extensions, $alerts=TRUE)
 file_unmunge_filename ($filename)
 file_create_filename ($basename, $directory)
 file_delete ($path)
 file_space_used ($uid=NULL)
 file_save_upload ($source, $validators=array(), $dest=FALSE, $replace=FILE_EXISTS_RENAME)
 file_validate_name_length ($file)
 file_validate_extensions ($file, $extensions)
 file_validate_size ($file, $file_limit=0, $user_limit=0)
 file_validate_is_image (&$file)
 file_validate_image_resolution (&$file, $maximum_dimensions=0, $minimum_dimensions=0)
 file_save_data ($data, $dest, $replace=FILE_EXISTS_RENAME)
 file_set_status (&$file, $status)
 file_transfer ($source, $headers)
 file_download ()
 file_scan_directory ($dir, $mask, $nomask=array('.', '..', 'CVS'), $callback=0, $recurse=TRUE, $key= 'filename', $min_depth=0, $depth=0)
 file_directory_temp ()
 file_directory_path ()
 file_upload_max_size ()

Detailed Description

Common file handling functions.

Enumeration Type Documentation

Flag used by file_create_directory() -- create directory if not present.

Definition at line 34 of file file.inc.

Flag to indicate that the 'private' file download method is enabled.

When using this method, all file requests are served by Drupal, during which access-control checking can be performed.

Definition at line 29 of file file.inc.

Flag to indicate that the 'public' file download method is enabled.

When using this method, files are available from a regular HTTP request, which provides no additional access restrictions.

Definition at line 21 of file file.inc.

Flag for dealing with existing files: Do nothing and return FALSE.

Definition at line 54 of file file.inc.

Flag for dealing with existing files: Append number until filename is unique.

Definition at line 44 of file file.inc.

Flag for dealing with existing files: Replace the existing file.

Definition at line 49 of file file.inc.

Flag used by file_create_directory() -- file permissions may be changed.

Definition at line 39 of file file.inc.

File status -- File has been permanently saved to the {files} tables.

If you wish to add custom statuses for use by contrib modules please expand as binary flags and consider the first 8 bits reserved. (0,1,2,4,8,16,32,64,128).

Definition at line 71 of file file.inc.

File status -- File has been temporarily saved to the {files} tables.

Drupal's file garbage collection will delete the file and remove it from the files table after a set period of time.

Definition at line 62 of file file.inc.


Function Documentation

file_check_directory ( &$  directory,
mode = 0,
form_item = NULL 
)

Check that the directory exists and is writable. Directories need to have execute permissions to be considered a directory by FTP servers, etc.

Parameters:
$directory A string containing the name of a directory path.
$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.
$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.
Returns:
FALSE when directory not found, or TRUE when directory exists.

Definition at line 136 of file file.inc.

References check_plain(), drupal_set_message(), file_directory_path(), file_directory_temp(), form_set_error(), t(), and watchdog().

Referenced by _locale_rebuild_js(), drupal_build_css_cache(), drupal_build_js_cache(), file_check_path(), DrupalWebTestCase::setUp(), system_theme_settings(), and user_admin_settings().

Here is the call graph for this function:

file_check_location ( source,
directory = '' 
)

Check if a file is really located inside $directory. Should be used to make sure a file specified is really located within the directory to prevent exploits.

   // Returns FALSE:
   file_check_location('/www/example.com/files/../../../etc/passwd', '/www/example.com/files');

Parameters:
$source A string set to the file to check.
$directory A string where the file should be located.
Returns:
0 for invalid path or the real path of the source.

Definition at line 219 of file file.inc.

Referenced by file_create_path().

file_check_path ( &$  path  ) 

Checks path to see if it is a directory, or a dir/file.

Parameters:
$path A string containing a file path. This will be set to the directory's path.
Returns:
If the directory is not in a Drupal writable directory, FALSE is returned. Otherwise, the base name of the path is returned.

Definition at line 189 of file file.inc.

References file_check_directory().

Referenced by file_copy(), and file_save_upload().

Here is the call graph for this function:

file_copy ( &$  source,
dest = 0,
replace = FILE_EXISTS_RENAME 
)

Copies a file to a new location. This is a powerful function that in many ways performs like an advanced version of copy().

  • Checks if $source and $dest are valid and readable/writable.
  • Performs a file copy if $source is not equal to $dest.
  • If file already exists in $dest either the call will error out, replace the file or rename the file based on the $replace parameter.

Parameters:
$source A string specifying the file location of the original file. This parameter will contain the resulting destination filename in case of success.
$dest A string containing the directory $source should be copied to. If this value is omitted, Drupal's 'files' directory will be used.
$replace Replace behavior when the destination file already exists.
  • FILE_EXISTS_REPLACE - Replace the existing file
  • FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique
  • FILE_EXISTS_ERROR - Do nothing and return FALSE.
Returns:
True for success, FALSE for failure.

Definition at line 254 of file file.inc.

References drupal_set_message(), file_check_path(), file_create_path(), file_destination(), t(), and watchdog().

Referenced by file_move(), and system_theme_settings().

Here is the call graph for this function:

file_create_filename ( basename,
directory 
)

Create a full file path from a directory and filename. If a file with the specified name already exists, an alternative will be used.

Parameters:
$basename string filename
$directory string directory
Returns:

Definition at line 443 of file file.inc.

References $name.

Referenced by file_destination().

file_create_path ( dest = 0  ) 

Make sure the destination is a complete path and resides in the file system directory, if it is not prepend the file system directory.

Parameters:
$dest A string containing the path to verify. If this value is omitted, Drupal's 'files' directory will be used.
Returns:
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).

Definition at line 102 of file file.inc.

References file_check_location(), file_directory_path(), and file_directory_temp().

Referenced by _locale_rebuild_js(), drupal_build_css_cache(), drupal_build_js_cache(), drupal_clear_css_cache(), drupal_clear_js_cache(), file_copy(), file_download(), file_save_upload(), file_transfer(), and user_admin_settings().

Here is the call graph for this function:

file_create_url ( path  ) 

Create the download path to a file.

Parameters:
$path A string containing the path of the file to generate URL for.
Returns:
A string containing a URL that can be used to download the file.

Definition at line 79 of file file.inc.

References file_directory_path(), url(), and variable_get().

Here is the call graph for this function:

file_delete ( path  ) 

Delete a file.

Parameters:
$path A string containing a file path.
Returns:
TRUE for success, FALSE for failure.

Definition at line 471 of file file.inc.

Referenced by _locale_rebuild_js(), and file_move().

file_destination ( destination,
replace 
)

Determines the destination path for a file depending on how replacement of existing files should be handled.

Parameters:
$destination A string specifying the desired path.
$replace Replace behavior when the destination file already exists.
  • FILE_EXISTS_REPLACE - Replace the existing file
  • FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique
  • FILE_EXISTS_ERROR - Do nothing and return FALSE.
Returns:
The destination file path or FALSE if the file already exists and FILE_EXISTS_ERROR was specified.

Definition at line 333 of file file.inc.

References drupal_set_message(), file_create_filename(), and t().

Referenced by file_copy(), and file_save_upload().

Here is the call graph for this function:

file_directory_path (  ) 

Determine the default 'files' directory.

Returns:
A string containing the path to Drupal's 'files' directory.

Definition at line 999 of file file.inc.

References conf_path(), and variable_get().

Referenced by drupal_get_css(), drupal_get_js(), file_check_directory(), file_create_path(), file_create_url(), file_directory_temp(), DrupalWebTestCase::setUp(), system_file_system_settings(), system_performance_settings(), system_theme_settings(), DrupalWebTestCase::tearDown(), and user_admin_settings().

Here is the call graph for this function:

file_directory_temp (  ) 

Determine the default temporary directory.

Returns:
A string containing a temp directory.

Definition at line 958 of file file.inc.

References file_directory_path(), variable_get(), and variable_set().

Referenced by file_check_directory(), file_create_path(), file_save_data(), file_save_upload(), and system_file_system_settings().

Here is the call graph for this function:

file_download (  ) 

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 returns -1 drupal_access_denied() will be returned. If one or more modules returned headers the download will start with the returned headers. If no modules respond drupal_not_found() will be returned.

Definition at line 865 of file file.inc.

References drupal_access_denied(), drupal_not_found(), file_create_path(), file_transfer(), and module_invoke_all().

Here is the call graph for this function:

file_move ( &$  source,
dest = 0,
replace = FILE_EXISTS_RENAME 
)

Moves a file to a new location.

  • Checks if $source and $dest are valid and readable/writable.
  • Performs a file move if $source is not equal to $dest.
  • If file already exists in $dest either the call will error out, replace the file or rename the file based on the $replace parameter.

Parameters:
$source A string specifying the file location of the original file. This parameter will contain the resulting destination filename in case of success.
$dest A string containing the directory $source should be copied to. If this value is omitted, Drupal's 'files' directory will be used.
$replace Replace behavior when the destination file already exists.
  • FILE_EXISTS_REPLACE - Replace the existing file
  • FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique
  • FILE_EXISTS_ERROR - Do nothing and return FALSE.
Returns:
True for success, FALSE for failure.

Definition at line 368 of file file.inc.

References drupal_set_message(), file_copy(), file_delete(), and t().

Referenced by file_save_data().

Here is the call graph for this function:

file_munge_filename ( filename,
extensions,
alerts = TRUE 
)

Munge the filename as needed for security purposes. For instance the file name "exploit.php.pps" would become "exploit.php_.pps".

Parameters:
$filename The name of a file to modify.
$extensions A space separated list of extensions that should not be altered.
$alerts Whether alerts (watchdog, drupal_set_message()) should be displayed.
Returns:
$filename The potentially modified $filename.

Definition at line 393 of file file.inc.

References drupal_set_message(), t(), and variable_get().

Referenced by file_save_upload().

Here is the call graph for this function:

file_save_data ( data,
dest,
replace = FILE_EXISTS_RENAME 
)

Save a string to the specified destination.

Parameters:
$data A string containing the contents of the file.
$dest A string containing the destination location.
$replace Replace behavior when the destination file already exists.
  • FILE_EXISTS_REPLACE - Replace the existing file
  • FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique
  • FILE_EXISTS_ERROR - Do nothing and return FALSE.
Returns:
A string containing the resulting filename or 0 on error

Definition at line 791 of file file.inc.

References drupal_set_message(), file_directory_temp(), file_move(), and t().

Referenced by _locale_rebuild_js(), drupal_build_css_cache(), and drupal_build_js_cache().

Here is the call graph for this function:

file_save_upload ( source,
validators = array(),
dest = FALSE,
replace = FILE_EXISTS_RENAME 
)

Saves a file upload to a new location. The source file is validated as a proper upload and handled as such.

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 file_set_status() to change its status.

Parameters:
$source A string specifying the name of the upload field to save.
$validators An optional, associative array of callback functions used to validate the file. The keys are function names and the values arrays of callback 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.
$dest 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.
$replace A boolean indicating whether an existing file of the same name in the destination directory should overwritten. A false value will generate a new, unique filename in the destination directory.
Returns:
An object containing the file information, or 0 in the event of an error.

Definition at line 518 of file file.inc.

References $name, drupal_set_message(), drupal_write_record(), file_check_path(), file_create_path(), file_destination(), file_directory_temp(), file_munge_filename(), file_upload_max_size(), form_set_error(), format_size(), t(), variable_get(), and watchdog().

Referenced by locale_translate_import_form_submit(), and system_theme_settings().

Here is the call graph for this function:

file_scan_directory ( dir,
mask,
nomask = array('.', '..', 'CVS'),
callback = 0,
recurse = TRUE,
key = 'filename',
min_depth = 0,
depth = 0 
)

Finds all files that match a given mask in a given directory. Directories and files beginning with a period are excluded; this prevents hidden files and directories (such as SVN working directories) from being scanned.

Parameters:
$dir The base directory for the scan, without trailing slash.
$mask The regular expression of the files to find.
$nomask An array of files/directories to ignore.
$callback The callback function to call for each match.
$recurse When TRUE, the directory scan will recurse the entire tree starting at the provided directory.
$key The key to be used for the returned array of files. Possible values are "filename", for the path starting with $dir, "basename", for the basename of the file, and "name" for the name of the file without an extension.
$min_depth Minimum depth of directories to return files from.
$depth Current depth of recursion. This parameter is only used internally and should not be passed.
Returns:
An associative array (keyed on the provided key) of objects with "path", "basename", and "name" members corresponding to the matching files.

Definition at line 920 of file file.inc.

References $depth, and $name.

Referenced by _drupal_rebuild_code_registry(), drupal_clear_css_cache(), drupal_clear_js_cache(), drupal_system_listing(), DrupalWebTestCase::drupalGetTestFiles(), DrupalTests::getFiles(), image_get_available_toolkits(), install_find_locales(), install_find_profiles(), locale_batch_by_component(), and locale_batch_by_language().

file_set_status ( &$  file,
status 
)

Set the status of a file.

Parameters:
file A Drupal file object
status A status value to set the file to.
Returns:
FALSE on failure, TRUE on success and $file->status will contain the status.

Definition at line 817 of file file.inc.

References $status, and db_query().

Here is the call graph for this function:

file_space_used ( uid = NULL  ) 

Determine total disk space used by a single user or the whole filesystem.

Parameters:
$uid An optional user id. A NULL value returns the total space used by all files.

Definition at line 484 of file file.inc.

References db_query(), and db_result().

Referenced by file_validate_size().

Here is the call graph for this function:

file_transfer ( source,
headers 
)

Transfer file using http to client. Pipes a file through Drupal to the client.

Parameters:
$source File to transfer.
$headers An array of http headers to send along with file.

Definition at line 832 of file file.inc.

References $header, drupal_not_found(), drupal_set_header(), exit, file_create_path(), and print.

Referenced by file_download().

Here is the call graph for this function:

file_unmunge_filename ( filename  ) 

Undo the effect of upload_munge_filename().

Parameters:
$filename string filename
Returns:
string

Definition at line 431 of file file.inc.

file_upload_max_size (  ) 

Determine the maximum file upload size by querying the PHP settings.

Returns:
A file size limit in bytes based on the PHP upload_max_filesize and post_max_size

Definition at line 1009 of file file.inc.

References parse_size().

Referenced by file_save_upload(), upload_admin_settings(), and upload_admin_settings_validate().

Here is the call graph for this function:

file_validate_extensions ( file,
extensions 
)

Check that the filename ends with an allowed extension. This check is not enforced for the user #1.

Parameters:
$file A Drupal file object.
$extensions A string with a space separated
Returns:
An array. If the file extension is not allowed, it will contain an error message.

Definition at line 657 of file file.inc.

References t().

Here is the call graph for this function:

file_validate_image_resolution ( &$  file,
maximum_dimensions = 0,
minimum_dimensions = 0 
)

If the file is an image verify that its dimensions are within the specified maximum and minimum dimensions. Non-image files will be ignored.

Parameters:
$file A Drupal file object. This function may resize the file affecting its size.
$maximum_dimensions An optional string in the form WIDTHxHEIGHT e.g. '640x480' or '85x85'. If an image toolkit is installed the image will be resized down to these dimensions. A value of 0 indicates no restriction on size, so resizing will be attempted.
$minimum_dimensions An optional string in the form WIDTHxHEIGHT. This will check that the image meets a minimum size. A value of 0 indicates no restriction.
Returns:
An array. If the file is an image and did not meet the requirements, it will contain an error message.

Definition at line 743 of file file.inc.

References drupal_set_message(), image_get_info(), image_get_toolkit(), image_scale(), and t().

Here is the call graph for this function:

file_validate_is_image ( &$  file  ) 

Check that the file is recognized by image_get_info() as an image.

Parameters:
$file A Drupal file object.
Returns:
An array. If the file is not an image, it will contain an error message.

Definition at line 714 of file file.inc.

References image_get_info(), and t().

Here is the call graph for this function:

file_validate_name_length ( file  ) 

Check for files with names longer than we can store in the database.

Parameters:
$file A Drupal file object.
Returns:
An array. If the file name is too long, it will contain an error message.

Definition at line 637 of file file.inc.

References t().

Here is the call graph for this function:

file_validate_size ( file,
file_limit = 0,
user_limit = 0 
)

Check that the file's size is below certain limits. This check is not enforced for the user #1.

Parameters:
$file A Drupal file object.
$file_limit An integer specifying the maximum file size in bytes. Zero indicates that no limit should be enforced.
$$user_limit An integer specifying the maximum number of bytes the user is allowed. Zero indicates that no limit should be enforced.
Returns:
An array. If the file size exceeds limits, it will contain an error message.

Definition at line 687 of file file.inc.

References file_space_used(), format_size(), and t().

Here is the call graph for this function:


Generated on Mon Jun 2 15:08:41 2008 for SimpleTest by  1.5.5