function field_file_save_file in FileField 6.3
Save a file into a file node after running all the associated validators.
This function is usually used to move a file from the temporary file directory to a permanent location. It may be used by import scripts or other modules that want to save an existing file into the database.
Parameters
$filepath: The local file path of the file to be saved.
$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.
$account: The user account object that should associated with the uploaded file.
Return value
An array containing the file information, or 0 in the event of an error.
1 call to field_file_save_file()
- _filefield_content_generate in ./
filefield.devel.inc - Private function used by filefield_content_generate().
File
- ./
field_file.inc, line 124 - Common functionality for file handling CCK field modules.
Code
function field_file_save_file($filepath, $validators = array(), $dest = FALSE, $account = NULL) {
if (!isset($account)) {
$account = $GLOBALS['user'];
}
// Add in our check of the the file name length.
$validators['file_validate_name_length'] = array();
// Begin building file object.
$file = new stdClass();
$file->uid = $account->uid;
$file->filename = basename($filepath);
$file->filepath = $filepath;
$file->filemime = module_exists('mimedetect') ? mimedetect_mime($file) : file_get_mimetype($file->filename);
// Rename potentially executable files, to help prevent exploits.
if (preg_match('/\\.(php|pl|py|cgi|asp|js)$/i', $file->filename) && substr($file->filename, -4) != '.txt') {
$file->filemime = 'text/plain';
$file->filepath .= '.txt';
$file->filename .= '.txt';
}
// If the destination is not provided, or is not writable, then use the
// temporary directory.
if (empty($dest) || file_check_path($dest) === FALSE) {
$dest = file_directory_temp();
}
$file->source = 'field_file_save_file';
$file->destination = file_destination(file_create_path($dest . '/' . $file->filename), FILE_EXISTS_RENAME);
$file->filesize = filesize($filepath);
// Call the validation functions.
$errors = array();
foreach ($validators as $function => $args) {
// Add the $file variable to the list of arguments and pass it by
// reference (required for PHP 5.3 and higher).
array_unshift($args, NULL);
$args[0] =& $file;
$errors = array_merge($errors, call_user_func_array($function, $args));
}
// Check for validation errors.
if (!empty($errors)) {
$message = t('The selected file %name could not be saved.', array(
'%name' => $file->filename,
));
if (count($errors) > 1) {
$message .= '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
}
else {
$message .= ' ' . array_pop($errors);
}
form_set_error($file->source, $message);
return 0;
}
if (!file_copy($file, $file->destination, FILE_EXISTS_RENAME)) {
form_set_error($file->source, t('File upload error. Could not move uploaded file.'));
watchdog('file', 'Upload error. Could not move file %file to destination %destination.', array(
'%file' => $file->filename,
'%destination' => $file->destination,
));
return 0;
}
// If we made it this far it's safe to record this file in the database.
$file->status = FILE_STATUS_TEMPORARY;
$file->timestamp = time();
// Insert new record to the database.
drupal_write_record('files', $file);
// Let modules add additional properties to the yet barebone file object.
foreach (module_implements('file_insert') as $module) {
$function = $module . '_file_insert';
$function($file);
}
_field_file_cache($file);
// cache the file in order to minimize load queries
return (array) $file;
}