Commit b2730e86 authored by webchick's avatar webchick
Browse files

#552066 by quicksketch and pwolanin: Fix bug with file_save_upload() and...

#552066 by quicksketch and pwolanin: Fix bug with file_save_upload() and trailing slashes (with tests).
parent af7cd743
......@@ -1067,7 +1067,7 @@ function file_space_used($uid = NULL, $status = FILE_STATUS_PERMANENT) {
* the status and use file_save() to save the changes.
*
* @param $source
* A string specifying the filepath or URI of the upload field to save.
* A string specifying the filepath or URI of the uploaded file to save.
* @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.
......@@ -1163,6 +1163,10 @@ function file_save_upload($source, $validators = array(), $destination = FALSE,
}
$file->source = $source;
// A URI may already have a trailing slash or look like "public://".
if (substr($destination, -1) != '/') {
$destination .= '/';
}
$file->destination = file_destination($destination . $file->filename, $replace);
// If file_destination() returns FALSE then $replace == FILE_EXISTS_ERROR and
// there's an existing file so we need to bail.
......
......@@ -603,11 +603,23 @@ class FileSaveUploadTest extends FileHookTestCase {
$this->assertTrue(isset($files[$file1->fid]), t('File was loaded successfully'));
$this->assertTrue(isset($files[$file2->fid]), t('File was loaded successfully'));
// Upload a third file to a subdirectory.
$image3 = current($this->drupalGetTestFiles('image'));
$image3_realpath = drupal_realpath($image3->uri);
$dir = $this->randomName();
$edit = array(
'files[file_test_upload]' => $image3_realpath,
'file_subdir' => $dir,
);
$this->drupalPost('file-test/upload', $edit, t('Submit'));
$this->assertResponse(200, t('Received a 200 response for posted test file.'));
$this->assertRaw(t('You WIN!'));
$this->assertTrue(is_file('temporary://' . $dir . '/' . trim(basename($image3_realpath))));
// Check that file_load_multiple() with no arguments returns FALSE.
$this->assertFalse(file_load_multiple(), t('No files were loaded.'));
}
/**
* Test renaming when uploading over a file that already exists.
*/
......
......@@ -54,6 +54,11 @@ function _file_test_form(&$form_state) {
),
'#default_value' => FILE_EXISTS_RENAME,
);
$form['file_subdir'] = array(
'#type' => 'textfield',
'#title' => 'Subdirectory for test image',
'#default_value' => '',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
......@@ -67,7 +72,14 @@ function _file_test_form(&$form_state) {
function _file_test_form_submit(&$form, &$form_state) {
// Process the upload and validate that it is an image. Note: we're using the
// form value for the $replace parameter.
$file = file_save_upload('file_test_upload', array('file_validate_is_image' => array()), FALSE, $form_state['values']['file_test_replace']);
if (!empty($form_state['values']['file_subdir'])) {
$destination = 'temporary://' . $form_state['values']['file_subdir'];
file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
}
else {
$destination = FALSE;
}
$file = file_save_upload('file_test_upload', array('file_validate_is_image' => array()), $destination, $form_state['values']['file_test_replace']);
if ($file) {
$form_state['values']['file_test_upload'] = $file;
drupal_set_message(t('File @filepath was uploaded.', array('@filepath' => $file->uri)));
......
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