• 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 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 preg_match_all – Get All Matches of Pattern in String
  • 2.  php Print Array – How to Print All Elements in Array
  • 3.  php property_exists() – Check if Property Exists in Class or Object
  • 4.  php sin – Find Sine of Number in Radians Using php sin() Function
  • 5.  PHP random_int() Function – Generate Random Integers in PHP
  • 6.  Get Query String from URL in php
  • 7.  Using isset() Function in php to Check if Variable is Defined
  • 8.  php array_intersect() – Find Intersection of Two or More Arrays in php
  • 9.  php unset – How to Destroy Variables in php
  • 10.  Replace Space with Dash in php String

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