You are here

function file_copy in Coder 6.2

Same name and namespace in other branches
  1. 5.2 scripts/coder_format/coder_format.php \file_copy()
  2. 6 scripts/coder_format/coder_format.php \file_copy()
  3. 7.2 scripts/coder_format/coder_format.php \file_copy()
  4. 7 scripts/coder_format/coder_format.php \file_copy()

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.

Parameters

$source A string specifying the file location of the original file.: This parameter will contain the resulting destination filename in case of success.

$dest A string containing the directory $source should be copied to.: If this value is omitted, Drupal's 'files' directory will be used.

$replace Replace behavior when the destination file already exists.:

Return value

True for success, FALSE for failure.

2 calls to file_copy()
coder_format_file in scripts/coder_format/coder_format.inc
Reads, backups, processes and writes the source code from and to a file.
file_move in scripts/coder_format/coder_format.php
Moves a file to a new location.

File

scripts/coder_format/coder_format.php, line 164
Coder format shell invocation script.

Code

function file_copy(&$source, $dest = 0, $replace = FILE_EXISTS_RENAME) {

  // $dest is almost always outside of Drupal. 23/01/2008 sun
  // $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');
    return 0;
  }

  // Removed upload handling. coder_format does not deal with uploads. 23/01/2008 sun
  $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 members
    // can alter files uploaded by the webserver.
    @chmod($dest, 0664);
  }

  // Removed upload handling. coder_format does not deal with uploads. 23/01/2008 sun
  $source = $dest;
  return 1;

  // Everything went ok.
}