ItSolutionStuff.com

PHP Change File Name in Folder Example

By Hardik Savani • May 14, 2024
PHP

Now, let's see post of php change file name in folder. 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.

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

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

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/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...

Tags: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

PHP Copy File from One Folder to Another Example

Read Now →

PHP Create Directory If It Doesn't Exist Example

Read Now →

How to Delete File from Public Folder / Storage Folder in Laravel?

Read Now →

Laravel Move File from One Folder to Another Example

Read Now →

Laravel Copy File from One Folder to Another Example

Read Now →

PHP Signature Pad Example | E-Signature Pad using Jquery Ajax and PHP

Read Now →

How to Get Difference Between Two Dates in PHP?

Read Now →

How to Convert String to Date in PHP?

Read Now →

JQuery Disable Right Click, Cut, Copy and Paste Example

Read Now →

How to Get Folder Path from File Path in PHP?

Read Now →

How to Set Value as Key in PHP Array?

Read Now →

How to Count Number of Files in a Directory in PHP?

Read Now →