How to move File from One Folder to Another in PHP?

If you need to move file from one folder to another using php code then we can use “rename()” function of php. php provide rename function to move your file from one place to another in php.

1. Code:

<?php

    function moveFileToOrtherFolder($filePath, $toFolder){
		if( !rename($filePath, $toFolder) ) {  
			echo "File can't be moved!";  
		}  
		else {  
			echo "File has been moved!";  
		} 
    }
	
	$filePath = 'D:/soltuts_new.txt';
	$toFolder = 'D:/soltuts/soltuts_new.txt';
    echo(moveFileToOrtherFolder($filePath, $toFolder));
?>

2. Example:

Input:

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

Output:

File has been moved!