function file_resup_save_upload in File Resumable Upload 8
Same name and namespace in other branches
- 7 file_resup.module \file_resup_save_upload()
Save a completed upload.
2 calls to file_resup_save_upload()
- file_resup_field_widget_value in ./
file_resup.field.inc - #file_value_callbacks callback for the field widget element.
- file_resup_value in ./
file_resup.field.inc - #value_callback callback for the resup element.
File
- ./
file_resup.module, line 225
Code
function file_resup_save_upload($element, $resup_file_id) {
global $user;
// Get a valid upload ID.
$upload_id = file_resup_upload_id($resup_file_id);
if (!$upload_id) {
return FALSE;
}
// Get the upload record.
$upload = file_resup_upload_load($upload_id);
if (!$upload) {
return FALSE;
}
// The file may have already been uploaded before.
if ($upload->fid) {
return file_load($upload->fid);
}
// Ensure the upload is complete.
if ($upload->uploaded_chunks != ceil($upload->filesize / file_resup_chunksize())) {
return FALSE;
}
// Ensure the destination is still valid.
$destination = $element['#upload_location'];
$destination_scheme = file_uri_scheme($destination);
if (!$destination_scheme || $destination_scheme != $upload->scheme) {
return FALSE;
}
// Ensure the uploaded file is present.
$upload_uri = file_resup_upload_uri($upload);
if (!file_exists($upload_uri)) {
return FALSE;
}
// Begin building the file object.
$file = new stdClass();
$file->uid = $user->uid;
$file->status = 0;
$file->filename = trim(drupal_basename($upload->filename), '.');
$file->uri = $upload_uri;
$file->filemime = file_get_mimetype($file->filename);
$file->filesize = $upload->filesize;
// Support Transliteration.
if (module_exists('transliteration') && variable_get('transliteration_file_uploads', TRUE)) {
$orig_filename = $file->filename;
$file->filename = transliteration_clean_filename($file->filename);
}
// Munge the filename.
$validators = $element['#file_resup_upload_validators'];
$extensions = '';
if (isset($validators['file_validate_extensions'])) {
if (isset($validators['file_validate_extensions'][0])) {
$extensions = $validators['file_validate_extensions'][0];
}
else {
unset($validators['file_validate_extensions']);
}
}
else {
$extensions = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp';
$validators['file_validate_extensions'][] = $extensions;
}
if (!empty($extensions)) {
$file->filename = file_munge_filename($file->filename, $extensions);
}
// Rename potentially executable files.
if (!variable_get('allow_insecure_uploads', 0) && preg_match('/\\.(php|pl|py|cgi|asp|js)(\\.|$)/i', $file->filename) && substr($file->filename, -4) != '.txt') {
$file->filemime = 'text/plain';
$file->uri .= '.txt';
$file->filename .= '.txt';
if (!empty($extensions)) {
$validators['file_validate_extensions'][0] .= ' txt';
drupal_set_message(t('For security reasons, your upload has been renamed to %filename.', array(
'%filename' => $file->filename,
)));
}
}
// Get the upload element name.
$element_parents = $element['#parents'];
if (end($element_parents) == 'resup') {
unset($element_parents[key($element_parents)]);
}
$form_field_name = implode('_', $element_parents);
// Run validators.
$validators['file_validate_name_length'] = array();
$errors = file_validate($file, $validators);
if ($errors) {
$message = t('The specified file %name could not be uploaded.', array(
'%name' => $file->filename,
));
if (count($errors) > 1) {
$message .= theme('item_list', array(
'items' => $errors,
));
}
else {
$message .= ' ' . array_pop($errors);
}
form_set_error($form_field_name, $message);
return FALSE;
}
// Prepare the destination directory.
if (!file_prepare_directory($destination, FILE_CREATE_DIRECTORY)) {
watchdog('file_resup', 'The upload directory %directory for the file field !name could not be created or is not accessible. A newly uploaded file could not be saved in this directory as a consequence, and the upload was canceled.', array(
'%directory' => $destination,
'!name' => $element['#field_name'],
));
form_set_error($form_field_name, t('The file could not be uploaded.'));
return FALSE;
}
// Complete the destination.
if (substr($destination, -1) != '/') {
$destination .= '/';
}
$destination = file_destination($destination . $file->filename, FILE_EXISTS_RENAME);
// Move the uploaded file.
$file->uri = $destination;
if (!rename($upload_uri, $file->uri)) {
form_set_error($form_field_name, t('File upload error. Could not move uploaded file.'));
watchdog('file_resup', 'Upload error. Could not move uploaded file %file to destination %destination.', array(
'%file' => $file->filename,
'%destination' => $file->uri,
));
return FALSE;
}
// Set the permissions on the new file.
drupal_chmod($file->uri);
// Transliteration support: restore the original filename if configured so.
if (isset($orig_filename) && !variable_get('transliteration_file_uploads_display_name', TRUE)) {
$file->filename = $orig_filename;
}
// Save the file object to the database.
$file->file_resup_filesize = $upload->filesize;
$file = file_save($file);
if (!$file) {
return FALSE;
}
// Update the upload record.
$upload->fid = $file->fid;
drupal_write_record('file_resup', $upload, 'upload_id');
return $file;
}