• 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
  • VBA
  • 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 Random Number Generator with rand Function
  • 2.  Check if String Ends with Given String in php with str_ends_with()
  • 3.  php floor – Round Down and Find the Floor of a Number in php
  • 4.  Remove HTML Tags from String in php
  • 5.  php var_dump() Function – Print Information about Variable Type and Value
  • 6.  php strlen() – Get the Length of String Variable in php
  • 7.  php acosh – Find Hyperbolic Arccosine of Number Using acosh() Function
  • 8.  How to Add Items to Array in php
  • 9.  php tanh – Find Hyperbolic Tangent of Number Using tanh() Function
  • 10.  php array_combine() – Create New Array Given Arrays of Keys and Values

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy