? .cache ? .project ? .projectOptions ? drupalhead_33482_0.patch ? files ? form.inc_121620_1.patch ? includes/Copy (2) of file.inc ? includes/Copy of file.inc ? misc/Thumbs.db ? misc/farbtastic/Thumbs.db ? sites/all/modules ? sites/default/settings.php Index: includes/file.inc =================================================================== RCS file: /cvs/drupal/drupal/includes/file.inc,v retrieving revision 1.99 diff -u -r1.99 file.inc --- includes/file.inc 30 May 2007 08:08:57 -0000 1.99 +++ includes/file.inc 30 May 2007 22:27:53 -0000 @@ -202,18 +202,52 @@ * - If file already exists in $dest either the call will error out, replace the * file or rename the file based on the $replace parameter. * + * @param $source A file object location of the original file. + * @param $dest A string containing the directory $source should be copied to. + * If this value is omitted, Drupal's 'files' directory will be used. + * @param $replace Replace behavior when the destination file already exists. + * - FILE_EXISTS_REPLACE - Replace the existing file + * - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique + * - FILE_EXISTS_ERROR - Do nothing and return FALSE. + * @return File object if the copy is successful, FALSE for failure. + */ +// TODO test this +function file_copy($source, $dest = 0, $replace = FILE_EXISTS_RENAME) { + if ($result = _file_copy($source, $dest, $replace)) { + $file = new stdClass(); + $file->uid = $source->uid; + $file->filename = basename($result); + $file->filepath = $result; + $file->filemime = $source->filemime; + $file->filesize = filesize($result); + $file->status = $source->status; + $file = file_save($file); + + module_invoke_all('file', 'copy', $file, $source); + + return $file; + } + return FALSE; +} + +/** + * Copies a file to a new location. This is a powerful function that in many ways + * performs like an advanced version of copy(). + * - Checks if $source and $dest are valid and readable/writable. + * - Performs a file copy if $source is not equal to $dest. + * - If file already exists in $dest either the call will error out, replace the + * file or rename the file based on the $replace parameter. + * * @param $source A string specifying the file location of the original file. - * This parameter will contain the resulting destination filename in case of - * success. * @param $dest A string containing the directory $source should be copied to. * If this value is omitted, Drupal's 'files' directory will be used. * @param $replace Replace behavior when the destination file already exists. * - FILE_EXISTS_REPLACE - Replace the existing file * - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique * - FILE_EXISTS_ERROR - Do nothing and return FALSE. - * @return True for success, FALSE for failure. + * @return The name of the new file, or FALSE for failure. */ -function file_copy(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME) { +function _file_copy($source, $dest = 0, $replace = FILE_EXISTS_RENAME) { $dest = file_create_path($dest); $directory = $dest; @@ -221,25 +255,16 @@ // Make sure we at least have a valid directory. if ($basename === FALSE) { - $source = is_object($source) ? $source->filepath : $source; + // TODO this error message makes no sense. this isn't used on uploads any longer. drupal_set_message(t('The selected file %file could not be uploaded, because the destination %directory is not properly configured.', array('%file' => $source, '%directory' => $dest)), 'error'); watchdog('file system', 'The selected file %file could not be uploaded, because the destination %directory could not be found, or because its permissions do not allow the file to be written.', array('%file' => $source, '%directory' => $dest), WATCHDOG_ERROR); - return 0; - } - - // Process a file upload object. - if (is_object($source)) { - $file = $source; - $source = $file->filepath; - if (!$basename) { - $basename = $file->filename; - } + return FALSE; } $source = realpath($source); if (!file_exists($source)) { drupal_set_message(t('The selected file %file could not be copied, because no file by that name exists. Please check that you supplied the correct filename.', array('%file' => $source)), 'error'); - return 0; + return FALSE; } // If the destination file is not specified then use the filename of the source file. @@ -257,7 +282,7 @@ if (!@copy($source, $dest)) { drupal_set_message(t('The selected file %file could not be copied.', array('%file' => $source)), 'error'); - return 0; + return FALSE; } // Give everyone read access so that FTP'd users or @@ -267,16 +292,7 @@ @chmod($dest, 0664); } - if (isset($file) && is_object($file)) { - $file->filename = $basename; - $file->filepath = $dest; - $source = $file; - } - else { - $source = $dest; - } - - return 1; // Everything went ok. + return $dest; } /** @@ -316,29 +332,31 @@ * - If file already exists in $dest either the call will error out, replace the * file or rename the file based on the $replace parameter. * - * @param $source A string specifying the file location of the original file. - * This parameter will contain the resulting destination filename in case of - * success. + * @param $source A file object for the original file. * @param $dest A string containing the directory $source should be copied to. * If this value is omitted, Drupal's 'files' directory will be used. * @param $replace Replace behavior when the destination file already exists. * - FILE_EXISTS_REPLACE - Replace the existing file * - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is unique * - FILE_EXISTS_ERROR - Do nothing and return FALSE. - * @return True for success, FALSE for failure. + * @return Resulting file object for success, FALSE for failure. */ -function file_move(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME) { - $path_original = is_object($source) ? $source->filepath : $source; - - if (file_copy($source, $dest, $replace)) { - $path_current = is_object($source) ? $source->filepath : $source; - - if ($path_original == $path_current || file_delete($path_original)) { - return 1; +// TODO test this +function file_move($source, $dest = 0, $replace = FILE_EXISTS_RENAME) { + if ($result = _file_copy($source->filepath, $dest, $replace)) { + if ($source->filepath == $result || _file_delete($source->filepath)) { + $file = drupal_clone($source); + $file->filename = basename($result); + $file->filepath = $result; + $file = file_save($file); + + module_invoke_all('file', 'move', $file, $source); + + return $file; } - drupal_set_message(t('The removal of the original file %file has failed.', array('%file' => $path_original)), 'error'); + drupal_set_message(t('The removal of the original file %file has failed.', array('%file' => $source->filepath)), 'error'); } - return 0; + return FALSE; } /** @@ -425,12 +443,30 @@ } /** + * Delete a file and its database record. + * + * @param $path A file object. + * @return TRUE for success, FALSE for failure. + */ +// TODO test this +function file_delete($file) { + if (_file_delete($file->filepath)) { + // TODO: delete the database record + + module_invoke_all('file', 'delete', $file); + return TRUE; + } + + return FALSE; +} + +/** * Delete a file. * * @param $path A string containing a file path. * @return TRUE for success, FALSE for failure. */ -function file_delete($path) { +function _file_delete($path) { if (is_file($path)) { return unlink($path); } @@ -972,3 +1008,29 @@ } return $max_size; } + +// TODO add PHPDocs +// TODO test this +function file_load($file_id) { + if (is_numeric($file_id)) { + $file = db_fetch_object(db_query('SELECT f.* FROM {files} f WHERE f.fid = %d'. $file_id)); + } + else { + $file = db_fetch_object(db_query("SELECT f.* FROM {files} f WHERE f.filepath = '%s'". $file_id)); + } + return $file; +} + +// TODO add PHPDocs +// TODO test this +function file_save($file) { + $file->timestamp = time(); + if (empty($file->fid)) { + $file->fid = db_next_id('{files}_fid'); + db_query("INSERT INTO {files} (fid, uid, filename, filepath, filemime, filesize, status, timestamp) VALUES (%d, %d, '%s', '%s', '%s', %d, %d, %d)", $file->fid, $file->uid, $file->filename, $file->filepath, $file->filemime, $file->filesize, $file->status, $file->timestamp); + } + else { + db_query("UPDATE {files} SET uid = %d, filename = '%s', filepath = '%s', filemime = '%s', filesize = %d, timestamp = %d WHERE fid = %d", $file->uid, $file->filename, $file->filepath, $file->filemime, $file->filesize, $file->timestamp, $file->fid); + } + return $file; +} \ No newline at end of file