1 file.inc | file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_EXISTS_RENAME) |
Internal function that prepares the destination for a file_unmanaged_copy or file_unmanaged_move operation.
- Checks if $source and $destination are valid and readable/writable.
- Checks that $source is not equal to $destination; if they are an error is reported.
- If file already exists in $destination either the call will error out, replace the file or rename the file based on the $replace parameter.
Parameters
$source: A string specifying the filepath or URI of the source file.
$destination: A URI containing the destination that $source should be moved/copied to. The URI may be a bare filepath (without a scheme) and in that case the default scheme (file://) will be used. If this value is omitted, Backdrop's default files scheme will be used, usually "public://".
$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.
Return value
TRUE, or FALSE in the event of an error.:
See also
Related topics
File
- core/
includes/ file.inc, line 952 - API for handling file uploads and server file management.
Code
function file_unmanaged_prepare($source, &$destination = NULL, $replace = FILE_EXISTS_RENAME) {
$original_source = $source;
// Assert that the source file actually exists.
if (!file_exists($source)) {
// @todo Replace backdrop_set_message() calls with exceptions instead.
backdrop_set_message(t('The specified file %file could not be moved/copied because no file by that name exists. Please check that you supplied the correct filename.', array('%file' => $original_source)), 'error');
if (($realpath = backdrop_realpath($original_source)) !== FALSE) {
watchdog('file', 'File %file (%realpath) could not be copied because it does not exist.', array('%file' => $original_source, '%realpath' => $realpath));
}
else {
watchdog('file', 'File %file could not be moved/copied because it does not exist.', array('%file' => $original_source));
}
return FALSE;
}
// Build a destination URI if necessary.
if (!isset($destination)) {
$destination = file_build_uri(backdrop_basename($source));
}
// Prepare the destination directory.
if (file_prepare_directory($destination)) {
// The destination is already a directory, so append the source basename.
$destination = file_stream_wrapper_uri_normalize($destination . '/' . backdrop_basename($source));
}
else {
// Perhaps $destination is a dir/file?
$dirname = backdrop_dirname($destination);
if (!file_prepare_directory($dirname)) {
// The destination is not valid.
watchdog('file', 'File %file could not be moved/copied because the destination directory %destination is not configured correctly.', array('%file' => $original_source, '%destination' => $dirname));
backdrop_set_message(t('The specified file %file could not be moved/copied because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', array('%file' => $original_source)), 'error');
return FALSE;
}
}
// Determine whether we can perform this operation based on overwrite rules.
$destination = file_destination($destination, $replace);
if ($destination === FALSE) {
backdrop_set_message(t('The file %file could not be moved/copied because a file by that name already exists in the destination directory.', array('%file' => $original_source)), 'error');
watchdog('file', 'File %file could not be moved/copied because a file by that name already exists in the destination directory (%directory)', array('%file' => $original_source, '%directory' => $destination));
return FALSE;
}
// Assert that the source and destination filenames are not the same.
$real_source = backdrop_realpath($source);
$real_destination = backdrop_realpath($destination);
if ($source == $destination || ($real_source !== FALSE) && ($real_source == $real_destination)) {
backdrop_set_message(t('The specified file %file was not moved/copied because it would overwrite itself.', array('%file' => $source)), 'error');
watchdog('file', 'File %file could not be moved/copied because it would overwrite itself.', array('%file' => $source));
return FALSE;
}
// Make sure the .htaccess files are present.
file_ensure_htaccess();
return TRUE;
}