=== modified file 'includes/file.inc' --- includes/file.inc 2009-01-22 13:00:13 +0000 +++ includes/file.inc 2009-01-30 16:28:04 +0000 @@ -283,14 +283,24 @@ // If the conditions array is populated, add those to the query. if ($conditions) { foreach ($conditions as $field => $value) { + // if the condition is a filepath, it may contain the path to the file_directory_path, so we need to remove this to allow the fid to be found + if ($field == 'filepath'){ + $value = (strpos($value, file_directory_path()) === 0) ? substr($value, strlen(file_directory_path()) + 1) : $value; + } $query->condition('f.' . $field, $value); } } $files = $query->execute()->fetchAllAssoc('fid'); - + // Invoke hook_file_load() on the terms loaded from the database // and add them to the static cache. if (!empty($files)) { + // prepend file_directory_path onto filepath + foreach ($files as $file){ + // add file_directory_path back in for the file_load hook, and return value + $file->filepath = file_directory_path() . '/' . $file->filepath; + } + foreach (module_implements('file_load') as $module) { $function = $module . '_file_load'; $function($files); @@ -332,9 +342,14 @@ function file_save($file) { $file = (object)$file; $file->timestamp = REQUEST_TIME; + $file->filesize = filesize($file->filepath); - - if (empty($file->fid)) { + + // remove file directory path for entering into the database + $old_filepath = $file->filepath; + $file->filepath = substr($file->filepath, strlen(file_directory_path()) + 1); + + if (empty($file->fid)) { drupal_write_record('files', $file); // Inform modules about the newly added file. module_invoke_all('file_insert', $file); @@ -345,6 +360,10 @@ module_invoke_all('file_update', $file); } + // and restore the original filepath dir for returning the file object + //should file_save just keep the filepath relevant to the files directory, and code outside of file.inc add the file_directory_path as necessary? + $file->filepath = $old_filepath; + return $file; } @@ -368,7 +387,7 @@ * @param $destination * A string containing the destination that $source should be copied to. This * can be a complete file path, a directory path or, if this value is omitted, - * Drupal's 'files' directory will be used. + * Drupal's 'files' directory will be used. N.B. it must be relative to the files directory * @param $replace * Replace behavior when the destination file already exists: * - FILE_EXISTS_REPLACE - Replace the existing file. If a managed file with @@ -385,6 +404,9 @@ */ function file_copy($source, $destination = NULL, $replace = FILE_EXISTS_RENAME) { $source = (object)$source; + + // ensure that the destination is inside the files dir + $destination = file_create_path($destination); if ($filepath = file_unmanaged_copy($source->filepath, $destination, $replace)) { $file = clone $source; @@ -410,7 +432,6 @@ // Inform modules that the file has been copied. module_invoke_all('file_copy', $file, $source); - return $file; } return FALSE; === modified file 'modules/simpletest/tests/file.test' --- modules/simpletest/tests/file.test 2009-01-20 03:00:13 +0000 +++ modules/simpletest/tests/file.test 2009-01-30 16:24:51 +0000 @@ -1,5 +1,5 @@ assertTrue(is_file($filepath), t('The test file exists on the disk.'), 'Create test file'); $file = new stdClass(); - $file->filepath = $filepath; + $file->filepath = substr($filepath, strlen(file_directory_path()) + 1); // we don't want a file_directory_path in the value that is written to the database $file->filename = basename($file->filepath); $file->filemime = 'text/plain'; $file->uid = 1; $file->timestamp = REQUEST_TIME; - $file->filesize = filesize($file->filepath); + $file->filesize = filesize($filepath); $file->status = 0; // Write the record directly rather than calling file_save() so we don't // invoke the hooks. $this->assertNotIdentical(drupal_write_record('files', $file), FALSE, t('The file was added to the database.'), 'Create test file'); + // but we really do want the file directory path available to the tests and other functions, so we need to put it back + $file->filepath = $filepath; return $file; } } @@ -1387,11 +1389,16 @@ */ function testSingleValues() { // Create a new file object from scratch so we know the values. + // as all paths are relative to the files dir, we can't use the old value of misc/druplicon.png because the path returned will include the path to the file_directory_path, where it doesn't exist + $filepath = file_directory_path() . '/' . $this->randomName(); + $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data."; + file_put_contents($filepath, $contents); + $file = array( 'uid' => 1, - 'filename' => 'druplicon.png', - 'filepath' => 'misc/druplicon.png', - 'filemime' => 'image/png', + 'filename' => basename($filepath), + 'filepath' => $filepath, + 'filemime' => 'text/plain', 'timestamp' => 1, 'status' => FILE_STATUS_PERMANENT, ); @@ -1413,11 +1420,16 @@ */ function testMultiple() { // Create a new file object. + // as all paths are relative to the files dir, we can't use the old value of misc/druplicon.png because the path returned will include the path to the file_directory_path + $filepath = file_directory_path() . '/' . $this->randomName(); + $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data."; + file_put_contents($filepath, $contents); + $file = array( 'uid' => 1, - 'filename' => 'druplicon.png', - 'filepath' => 'misc/druplicon.png', - 'filemime' => 'image/png', + 'filename' => basename($filepath), + 'filepath' => $filepath, + 'filemime' => 'text/plain', 'timestamp' => 1, 'status' => FILE_STATUS_PERMANENT, ); @@ -1457,15 +1469,20 @@ function testFileSave() { // Create a new file object. + // as all paths are relative to the files dir, we can't use the old value of misc/druplicon.png because the path returned will include the path to the file_directory_path + $filepath = file_directory_path() . '/' . $this->randomName(); + $contents = "file_put_contents() doesn't seem to appreciate empty strings so let's put in some data."; + file_put_contents($filepath, $contents); + $file = array( 'uid' => 1, - 'filename' => 'druplicon.png', - 'filepath' => 'misc/druplicon.png', - 'filemime' => 'image/png', + 'filename' => basename($filepath), + 'filepath' => $filepath, + 'filemime' => 'text/plain', 'timestamp' => 1, 'status' => FILE_STATUS_PERMANENT, ); - $file = (object) $file; + $file = file_save($file); // Save it, inserting a new record. $saved_file = file_save($file);