In order to Copy a file from one directory to another using PHP we can make use of copy() function,
bool copy ( string $srcLocation , string $dest [, resource $context ] )
The first parameter to copy function is the source location of the file, second parameter is the destination location.
Note : the third parameter is an $context is an optional parameter if you are passing it, it has to be a valid context resource created with stream_context_create() function,
Lets have a look at an example,
<?php
$srcFileName = 'fileA.php';
$copyFileName = 'fileACopy.php';
if (!copy($srcFileName, $copyFileName)) {
echo "Error occurred while copying file";
}
?>
Note : If the destination file already exists then the contents of the file will be overwritten.
copy() error: Permission denied : If you are getting such kind of error while trying to execute the copy function that make sure that you have provided the complete path with file Name for both the source and the destination folder. If you only provide the path for the destination folder you will get the error!!.
- PHP Code for sending Emails
- PHP drag and drop file upload tutorial using dropzone.js
- Delete file using PHP code : unlink()
- Save current timestamp in MySQL using PHP mysqi binding
- Failed to load resource: net::ERR_CACHE_MISS PHP
- PHP Warning: Cannot modify header information - headers already sent
- PHP header location function not called
- How to destroy PHP session()
- Upload docx file using PHP script
- Call PHP function on Button click using jquery ajax
- Upload Pdf file using PHP Script
- 403 forbidden error for Image
- PHP Base64 encoding decoding a variable
- PHP 301 Redirect Permanently
- Copy file from one directory to other in Php
- PHP Script to Upload Images to Server
- Installing vue.js in Laravel 8
- PHP Fatal error : Call to a member function bind_param() on a non-object
- PHP.ini: How to Remove URL Forward Slash Before Single or Double Quotes
- macOS - cannot calculate MAC address: Using fd 9 hv_vm_create HV_ERROR [PHP XAMPP]
- Syntax error, parameterized types are only available if source level is 1.5 or greater [Java] - Eclipse
- Display List of Databases in mysql Command Line - MySQL
- Set Falling Show on Website for Christmas using Pure CSS Code - CSS
- Change Font Size in Visual Studio Code - HowTos
- How to clear ZSH history of commands executed on Mac Terminal - zsh
- Find Duplicate Files using Mac Terminal Command - MacOS
- [fix] iCloud - Verification Failed: An unknown error occurred Apple ID - iOS
- WhatsApp launches WhatsApp Web to Access Messages over web browser - WhatsApp