Issue #1984378 by aroq, JacobSanford: argument is name of form field used to upload file, not 'filepath or URI of the uploaded file'.

8.0.x
Alex Pott 2013-05-27 20:50:00 -05:00
parent aeffeda1da
commit 08e824610c
1 changed files with 24 additions and 23 deletions

View File

@ -1026,8 +1026,9 @@ function file_unmanaged_delete_recursive($path, $callback = NULL) {
* Temporary files are periodically cleaned. Use file_usage()->add() to register
* the usage of the file which will automatically mark it as permanent.
*
* @param $source
* A string specifying the filepath or URI of the uploaded files to save.
* @param $form_field_name
* A string that is the associative array key of the upload form element in
* the form array.
* @param $validators
* An optional, associative array of callback functions used to validate the
* file. See file_validate() for a full discussion of the array format.
@ -1038,9 +1039,9 @@ function file_unmanaged_delete_recursive($path, $callback = NULL) {
* (Beware: this is not safe and should only be allowed for trusted users, if
* at all).
* @param $destination
* A string containing the URI $source should be copied to.
* This must be a stream wrapper URI. If this value is omitted, Drupal's
* temporary files scheme will be used ("temporary://").
* A string containing the URI that the file should be copied to. This must
* be a stream wrapper URI. If this value is omitted, Drupal's temporary
* files scheme will be used ("temporary://").
* @param $delta
* Delta of the file to save or NULL to save all files. Defaults to NULL.
* @param $replace
@ -1063,38 +1064,38 @@ function file_unmanaged_delete_recursive($path, $callback = NULL) {
* - source: Path to the file before it is moved.
* - destination: Path to the file after it is moved (same as 'uri').
*/
function file_save_upload($source, $validators = array(), $destination = FALSE, $delta = NULL, $replace = FILE_EXISTS_RENAME) {
function file_save_upload($form_field_name, $validators = array(), $destination = FALSE, $delta = NULL, $replace = FILE_EXISTS_RENAME) {
global $user;
static $upload_cache;
// Make sure there's an upload to process.
if (empty($_FILES['files']['name'][$source])) {
if (empty($_FILES['files']['name'][$form_field_name])) {
return NULL;
}
// Return cached objects without processing since the file will have
// already been processed and the paths in $_FILES will be invalid.
if (isset($upload_cache[$source])) {
if (isset($upload_cache[$form_field_name])) {
if (isset($delta)) {
return $upload_cache[$source][$delta];
return $upload_cache[$form_field_name][$delta];
}
return $upload_cache[$source];
return $upload_cache[$form_field_name];
}
// Prepare uploaded files info. Representation is slightly different
// for multiple uploads and we fix that here.
$uploaded_files = $_FILES;
if (!is_array($uploaded_files['files']['name'][$source])) {
if (!is_array($uploaded_files['files']['name'][$form_field_name])) {
foreach (array('name', 'type', 'tmp_name', 'error', 'size') as $value)
$uploaded_files['files'][$value][$source] = array($uploaded_files['files'][$value][$source]);
$uploaded_files['files'][$value][$form_field_name] = array($uploaded_files['files'][$value][$form_field_name]);
}
$files = array();
foreach ($uploaded_files['files']['name'][$source] as $i => $name) {
foreach ($uploaded_files['files']['name'][$form_field_name] as $i => $name) {
// Check for file upload errors and return FALSE for this file if a lower
// level system error occurred. For a complete list of errors:
// See http://php.net/manual/features.file-upload.errors.php.
switch ($uploaded_files['files']['error'][$source][$i]) {
switch ($uploaded_files['files']['error'][$form_field_name][$i]) {
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
drupal_set_message(t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', array('%file' => $name, '%maxsize' => format_size(file_upload_max_size()))), 'error');
@ -1110,7 +1111,7 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
case UPLOAD_ERR_OK:
// Final check that this is a valid upload, if it isn't, use the
// default error handler.
if (is_uploaded_file($uploaded_files['files']['tmp_name'][$source][$i])) {
if (is_uploaded_file($uploaded_files['files']['tmp_name'][$form_field_name][$i])) {
break;
}
@ -1126,8 +1127,8 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
'uid' => $user->uid,
'status' => 0,
'filename' => trim(drupal_basename($name, '.')),
'uri' => $uploaded_files['files']['tmp_name'][$source][$i],
'filesize' => $uploaded_files['files']['size'][$source][$i],
'uri' => $uploaded_files['files']['tmp_name'][$form_field_name][$i],
'filesize' => $uploaded_files['files']['size'][$form_field_name][$i],
);
$values['filemime'] = file_get_mimetype($values['filename']);
$file = entity_create('file', $values);
@ -1188,7 +1189,7 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
continue;
}
$file->source = $source;
$file->source = $form_field_name;
// A file URI may already have a trailing slash or look like "public://".
if (substr($destination, -1) != '/') {
$destination .= '/';
@ -1197,7 +1198,7 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
// If file_destination() returns FALSE then $replace === FILE_EXISTS_ERROR and
// there's an existing file so we need to bail.
if ($file->destination === FALSE) {
drupal_set_message(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $source, '%directory' => $destination)), 'error');
drupal_set_message(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', array('%source' => $form_field_name, '%directory' => $destination)), 'error');
$files[$i] = FALSE;
continue;
}
@ -1217,7 +1218,7 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
else {
$message .= ' ' . array_pop($errors);
}
form_set_error($source, $message);
form_set_error($form_field_name, $message);
$files[$i] = FALSE;
continue;
}
@ -1226,8 +1227,8 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
// directory. This overcomes open_basedir restrictions for future file
// operations.
$file->uri = $file->destination;
if (!drupal_move_uploaded_file($uploaded_files['files']['tmp_name'][$source][$i], $file->uri)) {
form_set_error($source, t('File upload error. Could not move uploaded file.'));
if (!drupal_move_uploaded_file($uploaded_files['files']['tmp_name'][$form_field_name][$i], $file->uri)) {
form_set_error($form_field_name, t('File upload error. Could not move uploaded file.'));
watchdog('file', 'Upload error. Could not move uploaded file %file to destination %destination.', array('%file' => $file->filename, '%destination' => $file->uri));
$files[$i] = FALSE;
continue;
@ -1251,7 +1252,7 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
}
// Add files to the cache.
$upload_cache[$source] = $files;
$upload_cache[$form_field_name] = $files;
return isset($delta) ? $files[$delta] : $files;
}