How to Change File Name from Folder in PHP?

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

1. Code:

<?php

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

2. Example:

Input:

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

Output:

"File has been renamed!