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!!.
- Delete file using PHP code : unlink()
- PHP header location function not called
- 403 forbidden error for Image
- Call PHP function on Button click using jquery ajax
- How to Pretty Print JSON in PHP
- Step-by-Step Guide: How to Fix - Error Establishing a Database Connection in WordPress
- PHP Base64 encoding decoding a variable
- 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
- PHP drag and drop file upload tutorial using dropzone.js
- Upload Pdf file using PHP Script
- PHP Warning: Cannot modify header information - headers already sent
- macOS - cannot calculate MAC address: Using fd 9 hv_vm_create HV_ERROR [PHP XAMPP]
- PHP Code for sending Emails
- How to destroy PHP session()
- Installing vue.js in Laravel 8
- PHP 301 Redirect Permanently
- Upload docx file using PHP script
- Save current timestamp in MySQL using PHP mysqi binding
- Copy file from one directory to other in Php
- Failed to load resource: net::ERR_CACHE_MISS PHP
- PHP Script to Upload Images to Server
- Disable Control Scroll Zoom-in and Zoom-out in Notepad++ - NotepadPlusPlus
- Background task activation is spurious error - Windows 10, Office 2016, Office 365 - HowTos
- Fix: ModuleNotFoundError: No module named azure-core - Azure
- What is HTTP 500 Internal Server Error Code on web browsers - HowTos
- How to install curl on Alpine Linux - Linux
- Add X days from today in Command Line - HowTos
- Fix: >>> pip install - SyntaxError: invalid syntax - PIP
- Java TLSv1.3 protocol code example using SSLSocket - Java