function media_add_upload_submit in D7 Media 7
Upload a file.
File
- includes/
media.pages.inc, line 221 - Common pages for the Media module.
Code
function media_add_upload_submit($form, &$form_state) {
$params = isset($form_state['build_info']['args'][0]) ? $form_state['build_info']['args'][0] : array();
$file = $form_state['values']['upload'];
// The media browser widget does not use the 'display' field.
$file->display = TRUE;
// Change the file status from temporary to permanent.
_media_save_file_permanently($file);
// Determine what URI scheme this file should use.
$scheme = !empty($params['uri_scheme']) && file_stream_wrapper_valid_scheme($params['uri_scheme']) ? $params['uri_scheme'] : file_default_scheme();
$scheme .= '://';
// Prepare the file's subdirectory path.
$directory = '';
if (!empty($params['file_directory'])) {
$directory = token_replace($params['file_directory']) . '/';
// If the directory isn't writable, or doesn't exist and can't be created,
// the upload will fail.
$prepare_directory = file_stream_wrapper_uri_normalize($scheme . $directory);
if (!file_prepare_directory($prepare_directory, FILE_CREATE_DIRECTORY)) {
drupal_set_message(t('The file directory @dir does not exist or is not writable. Please contact an administrator.', array(
'@dir' => $prepare_directory,
)), 'error');
return;
}
}
// Compose the file's permanent destination.
$destination = file_stream_wrapper_uri_normalize($scheme . $directory . $file->filename);
// Save the uploaded file to the permanent location.
$result = file_move($file, $destination, FILE_EXISTS_RENAME);
if ($result) {
drupal_set_message(t('The file @name was uploaded', array(
'@name' => $file->filename,
)));
}
else {
file_delete($file);
drupal_set_message(t('An error occurred and no file was uploaded.'), 'error');
return;
}
$form_state['redirect'] = array(
'media/browser',
array(
'query' => array(
'render' => 'media-popup',
'fid' => $file->fid,
),
),
);
}