diff --git a/includes/file.inc b/includes/file.inc index c0ce7c5942cedc68f2e68c4a5150014996e6b709..c0e68f52ebeb7f36106aa0b7ee01cf1d94ddb834 100644 --- a/includes/file.inc +++ b/includes/file.inc @@ -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. diff --git a/modules/simpletest/tests/file.test b/modules/simpletest/tests/file.test index 26bb051547f297e9e8fa9f35c109542555038672..2830e7b7ba07df1c9afab9cffe5b734573a35164 100644 --- a/modules/simpletest/tests/file.test +++ b/modules/simpletest/tests/file.test @@ -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. */ diff --git a/modules/simpletest/tests/file_test.module b/modules/simpletest/tests/file_test.module index 9670d1ab323ec597217c4d9c28462f704d6449cb..635576506c9eece6b56982b6344f8830b5420c71 100644 --- a/modules/simpletest/tests/file_test.module +++ b/modules/simpletest/tests/file_test.module @@ -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)));