Commit 08e82461 authored by alexpott's avatar alexpott

Issue #1984378 by aroq, JacobSanford: argument is name of form field used to...

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