• 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 / Using php to Copy Files Easily from One Directory to Another

Using php to Copy Files Easily from One Directory to Another

December 18, 2021 Leave a Comment

To copy a file using php, the easiest way is to use the php copy() function.

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

Copying files with php is easy. We can use the php copy() function to copy files.

If we want to copy a file named “file_name.txt” to “copied_file_name.txt”, we can do this with the following php code:

copy("./folder/file_name.txt","./folder/copied_file_name.txt");

One thing to note with the copy function, you need to make sure the target directory exists. If the target directory doesn’t exist, then the copy() function will not work.

Finally, if you want to move a file in php instead of copying, you can use the php rename() function.

Copying Multiple Files with php

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

Let’s say we have 5 files and we want to copy these 5 files from a folder named “folder” to another folder named “folder2”.

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 copy() function inside the loop:

$files_to_copy = 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++) {
  copy("./folder/" . $files_to_copy[$x], "./folder2/" . $new_file_names[$x]);
}

With this code, the 5 files will all be copied to the other directory.

Hopefully this article has been helpful for you to understand how you copy one file or copy multiple files using php.

Other Articles You'll Also Like:

  • 1.  php is_string() Function – Check if Variable is String in php
  • 2.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 3.  php min – Find Minimum Value of Array in php
  • 4.  php acosh – Find Hyperbolic Arccosine of Number Using acosh() Function
  • 5.  php Convert Radians to Degrees with php rad2deg() Function
  • 6.  Replace Underscore with Space in php String
  • 7.  php cosh – Find Hyperbolic Cosine of Number Using php cosh() Function
  • 8.  php array_pop() – Remove Last Element from Array
  • 9.  How to Add Items to Array in php
  • 10.  php str_replace – Replace String in php

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