Now, let is take a look at php posts change file name in folder. This article is detailed with a php name and rename it in a folder. We will look at an example of how to rename a file in php. Here you will learn how to change the file name uploaded to php. you will do the following things on how to change the file name using php.

If you need to rename a file in a php code folder then we can use the rename () php function. php provide rename function to rename your file from one location to another.

So, here I will give you a simple example with syntax on how it works to change a file name in a folder in php.

Let’s take a look at the below syntax and example that will help you.

Syntax

bool rename( string $source, string $destination, resource $context )

You can see the roaring parameter in detail:

$source: You need to provide the file path you want to rename.

$destination: you need to provide a local source file path.

$context: this option, Specifies the content service created for the stream_context_create () function.

Example

<?php
  
/* Existing File name */
$filePath = 'images/test.jpeg';
  
/* New File name */
$newFileName = 'images/test_new.jpeg';
  
/* Rename File name */
if( !rename($filePath, $newFileName) ) {  
    echo "File can't be renamed!";  
}  
else {  
    echo "File has been renamed!";  
} 
  
?>