How to Rename File Name in PHP?

In this example, i will show you how to change uploaded file name in php. This tutorial How To Rename File Name In PHP will give you simple example of how to change file name using php.We will look at example of php change file name in folder. Let’s get started with php rename file name in folder How to Rename File Name in PHP?

Solution 1:

If you need to rename file in folder php code then we can use “rename()” function How To Rename File Name In PHP of php another.

So, here i will give you very simple example syntax how it works to change file name in folder in php.

Let’s see bellow syntax and example that will help you.

Syntax like:

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

You can see bellow parameter in details:

$source: you need to give file path that you want to rename.

$destination: you need to give file path for destination source.

$context: this is optional, It specifies the context resource created with stream_context_create() function.

How to Rename File Name in PHP?

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!";  
} 
  
?>

I hope it can help you…

Leave a Comment