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!!.
- How to destroy PHP session()
- PHP Code for sending Emails
- 403 forbidden error for Image
- macOS - cannot calculate MAC address: Using fd 9 hv_vm_create HV_ERROR [PHP XAMPP]
- Upload Pdf file using PHP Script
- PHP header location function not called
- Copy file from one directory to other in Php
- PHP.ini: How to Remove URL Forward Slash Before Single or Double Quotes
- PHP Base64 encoding decoding a variable
- PHP Fatal error : Call to a member function bind_param() on a non-object
- PHP Script to Upload Images to Server
- Upload docx file using PHP script
- How to Pretty Print JSON in PHP
- PHP Warning: Cannot modify header information - headers already sent
- PHP drag and drop file upload tutorial using dropzone.js
- Failed to load resource: net::ERR_CACHE_MISS PHP
- PHP 301 Redirect Permanently
- Call PHP function on Button click using jquery ajax
- Installing vue.js in Laravel 8
- Save current timestamp in MySQL using PHP mysqi binding
- Delete file using PHP code : unlink()
- [fix] RabbitMQ: AuthenticationFailureException ACCESS_REFUSED Login refused authentication mechanism PLAIN - 2022
- Convert Javascript object to JSON String example - JavaScript
- 26: How to set Environment Variables using Python - Python
- Program 10: Modulo of Two Numbers - 1000+ Python Programs - Python-Programs
- Java: Create Temporary Directory and File and Delete when application terminates - Java
- Create RabbitMQ Exchange using Java Spring Boot Example - Java
- MySQL : Error :1000 SQLSTATE: HY000 (ER_HASHCHK) Message: hashchk Server Error - MySQL
- How to reset an Apple Watch without an iPhone - Apple