• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / php Rename File – Renaming Files and Directories Easily

php Rename File – Renaming Files and Directories Easily

December 17, 2021 Leave a Comment

To rename a file from one name to another using php, the easiest way is to use the php rename() function.

rename("./folder/file_name1.txt","./folder/file_name2.txt");

Renaming files with php is easy. We can use the php rename() function to rename files and rename directories.

If we want to rename a file named “old_file_name.txt” to “new_file_name.txt”, we can do this with the following php code:

rename("./folder/old_file_name.txt","./folder/new_file_name.txt");

We can also move the file from one folder to another and rename the file if we want. If we want to move the file from “folder1” to “folder2”, and rename the file, we can do this with the following php code:

rename("./folder1/old_file_name.txt","./folder2/new_file_name.txt");

If we want to rename a directory named “old_folder_name” to “new_folder_name”, we can do so with the following php code:

rename("./old_folder_name","./new_folder_name");

Renaming Multiple Files with php

In php, it is easy to rename multiple files in a folder. To do so, we can use the php rename() function in a loop.

Let’s say we have 5 files and we want to rename these 5 files.

We can put the 5 file names in an array, define a new array with the 5 new file names, and then loop over that array calling the rename() function inside the loop:

$files_to_rename = array("file1.php","file2.php","file3.php","file4.php","file5.php");

$new_file_names = array("file1_new.php","file2_new.php","file3_new.php","file4_new.php","file5_new.php");

for ($x = 0; $x < 5; $x++) {
  rename("./folder/" . $files_to_rename[$x], "./folder/" . $new_file_names[$x]);
}

With this code, the 5 files will all be renamed.

Renaming a Directory with php

We can also rename directories with the rename() function.

Let’s say we want to rename the folder named “edits_done” folder to a different folder named “production”.

We can move the directory easily with the php rename() function in the following php code:

rename("./edits_done","./production"

Hopefully this article has been helpful for you to understand how you rename a file, rename a directory and rename multiple files using php.

Other Articles You'll Also Like:

  • 1.  php asinh – Find Hyperbolic Arcsine of Number Using php asinh() Function
  • 2.  PHP Variables Are Preceded by $
  • 3.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]
  • 4.  php Move File from One Directory to Another Folder
  • 5.  php str_replace – Replace String in php
  • 6.  php sinh – Find Hyperbolic Sine of Number Using php sinh() Function
  • 7.  Check if Variable Exists in php with isset() Function
  • 8.  php cos – Find Cosine of Number in Radians Using php cos() Function
  • 9.  php range() – Create Array of Elements Within Given Range
  • 10.  php array_walk() – Modify Elements in Array with Callback Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy