An Essential Guide to PHP Copy File By Practical Examples (original) (raw)

Skip to content

Summary: in this tutorial, you will learn how to copy a file by using the PHP copy() file function from one location to another.

Introduction to the PHP copy() file function #

To copy a file from one location to another, you use the copy() function:

copy ( string <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>o</mi><mi>u</mi><mi>r</mi><mi>c</mi><mi>e</mi><mo separator="true">,</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi></mrow><annotation encoding="application/x-tex">source , string </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">so</span><span class="mord mathnormal">u</span><span class="mord mathnormal">rce</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span></span></span></span>dest , resource $context = ? ) : boolCode language: PHP (php)

The copy() file function has three parameters:

The copy() function returns true if the file is copied successfully or false in case there was an error during copying the file.

Note that if the $dest file exists, the copy() function will overwrite it.

Let’s take some examples of using the copy() function.

1) A simple PHP copy file example #

The following example uses the copy() function to copy the readme.txt to readme.bak file:

`<?php

$source = 'readme.txt'; $dest = 'readme.bak';

echo copy($source, $dest) ? "The file sourcewascopiedtosource was copied to sourcewascopiedtodest successfully!" : "Error copying the file $source";`Code language: HTML, XML (xml)

2) Check if the destination file exists before copying #

The following example uses the copy() function to copy the readme.txt file to the readme.bak file. Also, it checks if the readme.bak exists before overwriting the file:

`<?php

$source = 'readme.txt'; $dest = 'readme.bak';

!file_exists($source) && die("The file $source does not exist");

file_exists($dest) && die("The file $dest already exists");

echo copy($source, $dest) ? "The file sourcewascopiedtosource was copied to sourcewascopiedtodest successfully!" : "Error copying the file $source";`Code language: HTML, XML (xml)

3) PHP copy file helper function #

The following copy_file() helper function copies a file. It returns false if the source file does not exist or the destination file exists and the overwritten argument is true:

`<?php

function copy_file($source, dest,dest, dest,overwritten = true): bool { if (!file_exists($source)) { return false; }

if (!$overwritten && file_exists($dest)) {
    return false;
}

return copy($source, $dest);

}`Code language: HTML, XML (xml)

Summary #

Did you find this tutorial useful?