GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Downloads
  • Documentation
  • Get Involved
  • Help
  • Getting Started
  • Introduction
  • A simple tutorial
  • Language Reference
  • Basic syntax
  • Types
  • Variables
  • Constants
  • Expressions
  • Operators

This view

  • Original page
  • New address

GeezerVison is showing www.php.net — PHP: copy - Manual

PHP 8.6.0 RC5 available for testing
    delete »
    « clearstatcache
    • Preface
    • Function Reference
    • File System Related Extensions
    • Filesystem
    • Filesystem Functions

    copy

    (PHP 4, PHP 5, PHP 7, PHP 8)

    copy — Copies file

    Description

    function copy(string $from, string $to, ?resource $context = null): bool

    Makes a copy of the file from to to.

    If you wish to move a file, use the rename() function.

    Parameters

    from

    Path to the source file.

    to

    The destination path. If to is a URL, the copy operation may fail if the wrapper does not support overwriting of existing files.

    Warning

    If the destination file already exists, it will be overwritten.

    context

    A valid context resource created with stream_context_create().

    Return Values

    Returns true on success or false on failure.

    Examples

    Example #1 copy() example

    <?php
    $file = 'example.txt';
    $newfile = 'example.txt.bak';
    
    if (!copy($file, $newfile)) {
        echo "failed to copy $file...\n";
    }
    ?>

    See Also

    • move_uploaded_file() - Moves an uploaded file to a new location
    • rename() - Renames a file or directory
    • The section of the manual about handling file uploads

    Found A Problem?

    Learn How To Improve This Page • Submit a Pull Request • Report a Bug
    +add a note

    User Contributed Notes 11 notes

    up
    down
    251
    simonr_at_orangutan_dot_co_dot_uk ¶
    22 years ago
    Having spent hours tacking down a copy() error: Permission denied , (and duly worrying about chmod on winXP) , its worth pointing out that the 'destination' needs to contain the actual file name ! --- NOT just the path to the folder you wish to copy into.......
    DOH !
    hope this saves somebody hours of fruitless debugging
    up
    down
    140
    cooper at asu dot ntu-kpi dot kiev dot ua ¶
    20 years ago
    It take me a long time to find out what the problem is when i've got an error on copy(). It DOESN'T create any directories. It only copies to existing path. So create directories before. Hope i'll help,
    up
    down
    63
    steve a h ¶
    18 years ago
    Don't forget; you can use copy on remote files, rather than doing messy fopen stuff.  e.g.
    
    <?php
    if(!@copy('http://someserver.com/somefile.zip','./somefile.zip'))
    {
        $errors= error_get_last();
        echo "COPY ERROR: ".$errors['type'];
        echo "<br />\n".$errors['message'];
    } else {
        echo "File copied from remote!";
    }
    ?>
    up
    down
    27
    promaty at gmail dot com ¶
    15 years ago
    Here is a simple script that I use for removing and copying non-empty directories. Very useful when you are not sure what is the type of a file.
    
    I am using these for managing folders and zip archives for my website plugins.
    
    <?php
    
    // removes files and non-empty directories
    function rrmdir($dir) {
      if (is_dir($dir)) {
        $files = scandir($dir);
        foreach ($files as $file)
        if ($file != "." && $file != "..") rrmdir("$dir/$file");
        rmdir($dir);
      }
      else if (file_exists($dir)) unlink($dir);
    } 
    
    // copies files and non-empty directories
    function rcopy($src, $dst) {
      if (file_exists($dst)) rrmdir($dst);
      if (is_dir($src)) {
        mkdir($dst);
        $files = scandir($src);
        foreach ($files as $file)
        if ($file != "." && $file != "..") rcopy("$src/$file", "$dst/$file"); 
      }
      else if (file_exists($src)) copy($src, $dst);
    }
    ?>
    
    Cheers!
    up
    down
    27
    absorbentshoulderman at gmail dot com ¶
    13 years ago
    A nice simple trick if you need to make sure the folder exists first:
    
    <?php
    
    $srcfile='C:\File\Whatever\Path\Joe.txt';
    $dstfile='G:\Shared\Reports\Joe.txt';
    mkdir(dirname($dstfile), 0777, true);
    copy($srcfile, $dstfile);
    
    ?>
    
    That simple.
    up
    down
    3
    someone at terrasim dot com ¶
    5 years ago
    On Windows, php-7.4.19-Win32-vc15-x64 - copy() corrupted a 6GB zip file. Our only recourse was to write:
    
    function file_win_copy( $src, $dst ) {
     shell_exec( 'COPY "'.$src.'" "'.$dst.'"');
     return file_exists($dest);
    }
    up
    down
    19
    gimmicklessgpt at gmail dot com ¶
    17 years ago
    Here's a simple recursive function to copy entire directories
    
    Note to do your own check to make sure the directory exists that you first call it on.
    
    <?php
    function recurse_copy($src,$dst) {
        $dir = opendir($src);
        @mkdir($dst);
        while(false !== ( $file = readdir($dir)) ) {
            if (( $file != '.' ) && ( $file != '..' )) {
                if ( is_dir($src . '/' . $file) ) {
                    recurse_copy($src . '/' . $file,$dst . '/' . $file);
                }
                else { 
                    copy($src . '/' . $file,$dst . '/' . $file);
                }
            }
        }
        closedir($dir);
    }
    ?>
    up
    down
    6
    tom at r dot je ¶
    18 years ago
    It's worth noting that copy() sets the destination file's last modified time/date.
    up
    down
    3
    hugo_2000 at gmx dot at ¶
    11 years ago
    If you try to copy a file to itself - e.g. if the target directory is just a symlink to the source directory - copy will return false. just like on the command line.
    up
    down
    5
    eng-ayman at aymax dot net ¶
    17 years ago
    some hosts disable copy() function and say its for security 
    
    and for some copy is important so this is and simple function that do same as copy function effect 
    
    how smart php can help us l like php 
    
    <?php
        function copyemz($file1,$file2){
              $contentx =@file_get_contents($file1);
                       $openedfile = fopen($file2, "w");
                       fwrite($openedfile, $contentx);
                       fclose($openedfile);
                        if ($contentx === FALSE) {
                        $status=false;
                        }else $status=true;
                        
                        return $status;
        }
    ?>
    up
    down
    1
    Vinicio Coletti ¶
    10 years ago
    Copying large files under Windows 8.1, from one NTFS filesystem to another NTFS filesystem, results in only the first 4 GiB copied and the rest of the file is ignored.
    
    So, if you think to have files larger than 4 GiB, instead of doing:
       copy($source,$destination);
    it is much better to do something like:
       exec("xcopy $source $destination");
    
    I will check to see if this issue is valid also under Linux.
    It depends on PHP not being compiled in 64 bit mode?
    +add a note
    • Filesystem Functions
      • basename
      • chgrp
      • chmod
      • chown
      • clearstatcache
      • copy
      • delete
      • dirname
      • disk_​free_​space
      • disk_​total_​space
      • diskfreespace
      • fclose
      • fdatasync
      • feof
      • fflush
      • fgetc
      • fgetcsv
      • fgets
      • fgetss
      • file
      • file_​exists
      • file_​get_​contents
      • file_​put_​contents
      • fileatime
      • filectime
      • filegroup
      • fileinode
      • filemtime
      • fileowner
      • fileperms
      • filesize
      • filetype
      • flock
      • fnmatch
      • fopen
      • fpassthru
      • fputcsv
      • fputs
      • fread
      • fscanf
      • fseek
      • fstat
      • fsync
      • ftell
      • ftruncate
      • fwrite
      • glob
      • is_​dir
      • is_​executable
      • is_​file
      • is_​link
      • is_​readable
      • is_​uploaded_​file
      • is_​writable
      • is_​writeable
      • lchgrp
      • lchown
      • link
      • linkinfo
      • lstat
      • mkdir
      • move_​uploaded_​file
      • parse_​ini_​file
      • parse_​ini_​string
      • pathinfo
      • pclose
      • popen
      • readfile
      • readlink
      • realpath
      • realpath_​cache_​get
      • realpath_​cache_​size
      • rename
      • rewind
      • rmdir
      • set_​file_​buffer
      • stat
      • symlink
      • tempnam
      • tmpfile
      • touch
      • umask
      • unlink
    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.