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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • 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 str_replace – Replace String in php
  • 2.  PHP Numerically Indexed Array Begin With Position 0
  • 3.  Get Last Element of Array in php
  • 4.  Replace Space with Dash in php String
  • 5.  php Rename File – Renaming Files and Directories Easily
  • 6.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]
  • 7.  php tan – Find Tangent of Number in Radians Using php tan() Function
  • 8.  php print_r() Function- Get Information about Variables in php
  • 9.  Get Current Month of Date in php
  • 10.  php switch case – How to Use Switch…Case Statements 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

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