def copy(source, dest=0, replace=FILE_EXISTS_RENAME): """ 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. """ php.Reference.check(source) dest = file_create_path(dest) directory = dest basename = file_check_path(directory) # Make sure we at least have a valid directory. if (basename == False): if hasattr(source, 'filepath'): source._ = source.filepath drupal_set_message(t('The selected file %file could not be ' + \ 'uploaded, because the destination %directory is not ' + \ 'properly configured.', \ {'%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.', \ {'%file' : source._, '%directory' : dest}, WATCHDOG_ERROR) return False # Process a file upload object. if (php.is_object(source._)): file = source._ source._ = file.filepath if (not basename): basename = file.filename source._ = php.realpath(source._) if (not php.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.', \ {'%file' : source._}), 'error') return False # If the destination file is not specified then use the filename # of the source file. basename = (basename if basename else 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._ != php.realpath(dest._)): dest._ = file_destination(dest._, replace) if (not dest._): drupal_set_message(t('The selected file %file could not be copied, ' + \ 'because a file by that name already exists in the destination.', \ {'%file' : source._}), 'error') return False if (not copy(source._, dest._)): drupal_set_message(t('The selected file %file could not be copied.', \ {'%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 members # can alter files uploaded by the webserver. chmod(dest._, 0664) if (php.isset(file) and php.is_object(file)): file.filename = basename file.filepath = dest._ source._ = file else: source._ = dest return True # Everything went ok.
def copy(source, dest = 0, replace = FILE_EXISTS_RENAME): """ 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. """ php.Reference.check(source) dest = file_create_path(dest) directory = dest basename = file_check_path(directory) # Make sure we at least have a valid directory. if (basename == False): if hasattr(source, 'filepath'): source._ = source.filepath drupal_set_message(t('The selected file %file could not be ' + \ 'uploaded, because the destination %directory is not ' + \ 'properly configured.', \ {'%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.', \ {'%file' : source._, '%directory' : dest}, WATCHDOG_ERROR) return False # Process a file upload object. if (php.is_object(source._)): file = source._ source._ = file.filepath if (not basename): basename = file.filename source._ = php.realpath(source._) if (not php.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.', \ {'%file' : source._}), 'error') return False # If the destination file is not specified then use the filename # of the source file. basename = (basename if basename else 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._ != php.realpath(dest._)): dest._ = file_destination(dest._, replace) if (not dest._): drupal_set_message(t('The selected file %file could not be copied, ' + \ 'because a file by that name already exists in the destination.', \ {'%file' : source._}), 'error') return False if (not copy(source._, dest._)): drupal_set_message(t('The selected file %file could not be copied.', \ {'%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 members # can alter files uploaded by the webserver. chmod(dest._, 0664) if (php.isset(file) and php.is_object(file)): file.filename = basename file.filepath = dest._ source._ = file else: source._ = dest return True # Everything went ok.