How to copy file from one folder to another in PHP?

In this post, we will give you very simple example and syntax how it works to copy file from one folder to another in php.

1. Code:

<?php

    function copyFileToOrtherFolder($filePath, $destinationFilePath){
		if( !copy($filePath, $destinationFilePath) ) {  
			echo "File can't be copied!";  
		}  
		else {  
			echo "File has been copied!";  
		} 
    }
	
	$filePath = 'D:/soltuts_new.txt';
	$destinationFilePath = 'D:/soltuts/soltuts_new.txt';
    echo(copyFileToOrtherFolder($filePath, $destinationFilePath));
?>

2. Example:

Input:

$filePath = 'D:/soltuts_new.txt';
$destinationFilePath = 'D:/soltuts/soltuts_new.txt';

Output:

File has been copied!