summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/file.inc6
-rw-r--r--modules/simpletest/tests/file.test14
-rw-r--r--modules/simpletest/tests/file_test.module14
3 files changed, 31 insertions, 3 deletions
diff --git a/includes/file.inc b/includes/file.inc
index c0ce7c594..c0e68f52e 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 26bb05154..2830e7b7b 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 9670d1ab3..635576506 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)));