Home php How to Change File Name by php?

How to Change File Name by php?

by Shahriar Sagor

Hello all
Welcome back. Now, let’s start a new post of php how to change file name in folder. In this article I goes in detailed on php rename file name in folder. So i will look at a small example of how to rename file name in php. So here you will learn about how to change uploaded file name by using php. We will do now the following things for how to Change File Name by php.

If you need to change or rename file in folder by using php code, then we can do easily by use “rename()” function of php. Php provide us rename function to rename our file from one place to another in folder.

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

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


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

You can see bellow few parameters in details:

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

$destination: next we need to give file path for our destination source.

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

Read Also : How to replace image src in a dynamic HTML string?


/* 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!";  

Hope it will help you………. Also you can follow us on Facebook .


You may also like