PHP Change File Name in Folder Tutorial

PHP 8 , PHP

Nicesnippets

1150

12-04-2021


PHP Change File Name in Folder Tutorial

Hi Guys,

Today, I will learn you how to change file name in folder using php. This article goes in detailed on php rename file name in folder. We will look at example of how to rename file name in php. Here you will learn how to change uploaded file name in php. you will do the following things for how to change file name using 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.

Below, I will give you very simple example and syntax how it works to change file name in folder in php.

Syntax:

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.

Example:


<?php

/* Existing File name */

$filePath = 'images/demo.jpeg';

/* New File name */

$newFileName = 'images/demo_new.jpeg';

/* Rename File name */

if( !rename($filePath, $newFileName) ) {

echo "File can't be renamed!";

}

else {

echo "File has been renamed!";

}

?>

It will help you..


Recommended Posts