• 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 Move File from One Directory to Another Folder

php Move File from One Directory to Another Folder

December 17, 2021 Leave a Comment

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

rename("./folder1/file.txt","./folder2/file.txt");

Moving files with php is easy. We can use the php rename() function to move a file into a different folder or directory.

If we want to move a file named “file.txt” from “folder1” to “folder2”, we can do this with the following code:

rename("./folder1/file.txt","./folder2/file.txt");

We can also change the destination file name if we want. If we want to rename the file name from “file.txt” to “new_name.txt”, we can do so in the following php code:

rename("./folder1/file.txt","./folder2/new_name.txt");

Moving Multiple Files to a Different Folder in php

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

Let’s say we have 5 files and we want to move these 5 files from the “development” folder to the “production” folder.

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

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

for ($x = 0; $x < 5; $x++) {
  rename("./development/" . $array_of_file_names[$x], "./production/" . $array_of_file_names[$x]);
}

With this code, the 5 files will move to the new folder.

Moving a Directory with php

We can also move directories to new locations with the rename() function.

Let’s say we want to move the “assets” folder from 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/assets","./production/assets"

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

Other Articles You'll Also Like:

  • 1.  php acos – Find Arccosine and Inverse Cosine of Number
  • 2.  php json_encode() Function – Get JSON Representation of Variables in php
  • 3.  Get Current Month of Date in php
  • 4.  Delete Cookie Using php
  • 5.  php pi – Get Value of pi Using php pi() Function
  • 6.  What is the Difference Between unset() and unlink() in php?
  • 7.  Using strtoupper() in php to Convert String to Uppercase
  • 8.  php session_destroy() – Delete Session File Where Session Data is Stored
  • 9.  How to Get Current System IP Address in php
  • 10.  Capitalize First Letter of String with php ucfirst() 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

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