PHP: Copy() Function Example

PHP: Copy() Function Example

In this tutorial, we will teach you about PHP’s copy () function and show you how to use PHP copy function.

At the same time, we will also explain the definition, syntax, parameters of copy () and several examples of copy () of PHP. Which will help you a lot in understanding how the copy function works. And how can you use it?

PHP: Copy() Function Example

This copy function is a pre-built function of PHP. Mainly that which is used to copy a file. It copies the given file to the destination file. If the destination file already exists. It is overwritten. The copy () function returns false on fail and true on success.

syntax

The syntax of php copy() funcition is following

copy ( $source file, $destination file )

Note:- This function takes 2 parameters, first source file, and second destination file.

Parameters description

$source file: It specifies the path to the source file.
$destination file: It is used to specify the path to the destination file.

Return Value: It returns false on fail and true on success.

Example 1 :- PHP copy file

To copy a file from one folder to the same folder. We can easily create a copy or duplicate file to original in PHP with PHP’s built-in function called copy().

<?php 
  
// Copying text.txt to newText.txt 

echo copy("text.txt", "newText.txt"); 
  
?> 

Result of the above code is the following:

True

Example 2:- PHP copy file to another directory and rename

To copy a file from one folder to another directory folder.We can easily create a copy or duplicate file to original in PHP with PHP’s built-in function called copy().

<?php 
  
// Copying text.txt to newText.txt 

$sourceFile = '/user01/Desktop/myFolder/text.txt'; 

$destinationFile = 'user01/Desktop/myFolder2/newText.txt'; 
  
if (!copy($sourceFile, $destinationFile)) { 

    echo "File has not been copied! \n"; 
} 
else { 

    echo "File has been copied!"; 
} 
  
?> 

Result of the above code is the following:

File has been copied!

Example 3:- copy image from one folder to another in PHP

To copy a file from one folder to another or the same folder.We can easily create a copy or duplicate file to original in PHP with PHP’s built-in function called copy().

<?php

$file = '/usr/home/myFolder/image.png';

$newfile = '/usr/home/myFolder/image.png';
 
if (!copy($file, $newfile)) {

    echo "failed to copy $file...\n";

} else {

    echo "copied $file into $newfile\n";
}

?>

Recommended PHP Tutorial:

  1. Autocomplete Search Box in PHP MySQL
  2. Compare Arrays PHP | PHP array_diff() Function
  3. Get, Write, Read, Load, JSON File from Url PHP
  4. Functions: Remove First Character From String PHP
  5. Remove Specific/Special Characters From String In PHP
  6. How to Replace First and Last Character From String PHP
  7. Reverse String in PHP
  8. Array Push, POP PHP | PHP Array Tutorial
  9. PHP Search Multidimensional Array By key, value and return key
  10. json_encode()- Convert Array To JSON | Object To JSON PHP
  11. PHP remove duplicates from multidimensional array
  12. PHP Remove Duplicate Elements or Values from Array PHP
  13. Get Highest Value in Multidimensional Array PHP
  14. PHP Get Min or Minimum Value in Array

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *