? .cache ? .project ? .projectOptions ? drupalhead_33482_0.patch ? files ? form.inc_121620_1.patch ? 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 17:28:25 -0000 @@ -202,81 +202,28 @@ * - 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 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 True for success, FALSE for failure. + * @return File object if the copy is successful, FALSE for failure. */ -function file_copy(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME) { - $dest = file_create_path($dest); - - $directory = $dest; - $basename = file_check_path($directory); - - // Make sure we at least have a valid directory. - if ($basename === FALSE) { - $source = is_object($source) ? $source->filepath : $source; - 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; - } - } - - $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; - } - - // If the destination file is not specified then use the filename of the source file. - $basename = $basename ? $basename : basename($source); - $dest = $directory .'/'. $basename; - - // Make sure source and destination filenames are not the same, makes no sense - // to copy it if they are. In fact copying the file will most likely result in - // a 0 byte file. Which is bad. Real bad. - if ($source != realpath($dest)) { - if (!$dest = file_destination($dest, $replace)) { - drupal_set_message(t('The selected file %file could not be copied, because a file by that name already exists in the destination.', array('%file' => $source)), 'error'); - return FALSE; - } - - if (!@copy($source, $dest)) { - drupal_set_message(t('The selected file %file could not be copied.', array('%file' => $source)), 'error'); - return 0; - } - - // Give everyone read access so that FTP'd users or - // non-webserver users can see/read these files, - // and give group write permissions so group memebers - // can alter files uploaded by the webserver. - @chmod($dest, 0664); - } - - if (isset($file) && is_object($file)) { - $file->filename = $basename; - $file->filepath = $dest; - $source = $file; - } - else { - $source = $dest; +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 1; // Everything went ok. + return FALSE; } /** @@ -316,29 +263,28 @@ * - 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; +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; } /** @@ -427,13 +373,12 @@ /** * Delete a file. * - * @param $path A string containing a file path. + * @param $path A file object. * @return TRUE for success, FALSE for failure. */ -function file_delete($path) { - if (is_file($path)) { - return unlink($path); - } +function file_delete($file) { + module_invoke_all('file', 'delete', $file); + return _file_delete($file->filepath); } /** @@ -574,8 +519,9 @@ } // If we made it this far it's safe to record this file in the database. - $file->fid = db_next_id('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, $user->uid, $file->filename, $file->filepath, $file->filemime, $file->filesize, FILE_STATUS_TEMPORARY, time()); + $file->uid = $user->uid; + $file->status = FILE_STATUS_TEMPORARY; + file_save($file); // Add file to the cache. $upload_cache[$source] = $file; @@ -972,3 +918,105 @@ } return $max_size; } + + + +/** + * 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. + * @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 FALSE for failure, otherwise the name of the new file. + */ +function _file_copy($source, $dest = 0, $replace = FILE_EXISTS_RENAME) { + $dest = file_create_path($dest); + + $directory = $dest; + $basename = file_check_path($directory); + + // Make sure we at least have a valid directory. + if ($basename === FALSE) { + // TODO this error message makes no sense. this isn't used on uploads any longer. + $source = is_object($source) ? $source->filepath : $source; + 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 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 FALSE; + } + + // If the destination file is not specified then use the filename of the source file. + $basename = $basename ? $basename : basename($source); + $dest = $directory .'/'. $basename; + + // Make sure source and destination filenames are not the same, makes no sense + // to copy it if they are. In fact copying the file will most likely result in + // a 0 byte file. Which is bad. Real bad. + if ($source != realpath($dest)) { + if (!$dest = file_destination($dest, $replace)) { + drupal_set_message(t('The selected file %file could not be copied, because a file by that name already exists in the destination.', array('%file' => $source)), 'error'); + return FALSE; + } + + if (!@copy($source, $dest)) { + drupal_set_message(t('The selected file %file could not be copied.', array('%file' => $source)), 'error'); + return FALSE; + } + + // Give everyone read access so that FTP'd users or + // non-webserver users can see/read these files, + // and give group write permissions so group memebers + // can alter files uploaded by the webserver. + @chmod($dest, 0664); + } + + return $dest; +} + +/** + * Delete a file. + * + * @param $path A string containing a file path. + * @return TRUE for success, FALSE for failure. + */ +function _file_delete($path) { + if (is_file($path)) { + return unlink($path); + } +} + +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; +} + +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