Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home PHP PHP Copy Entire Directory To Another Directory

PHP Copy Entire Directory To Another Directory

This is a PHP function that copy an entire specified directory/folder includes its sub directories/folders and files to another folder/directory.

1. PHP Copy Entire Directory Recursively

<?php
	function copy_directory( $source, $destination ) {
		if ( is_dir( $source ) ) {
			@mkdir( $destination );
			$directory = dir( $source );
			while ( FALSE !== ( $readdirectory = $directory->read() ) ) {
				if ( $readdirectory == '.' || $readdirectory == '..' ) {
					continue;
				}
				$PathDir = $source . '/' . $readdirectory;
				if ( is_dir( $PathDir ) ) {
					copy_directory( $PathDir, $destination . '/' . $readdirectory );
					continue;
				}
				copy( $PathDir, $destination . '/' . $readdirectory );
			}
 
			$directory->close();
		}
		else {
			copy( $source, $destination );
		}
	}
?>

<?php function copy_directory( $source, $destination ) { if ( is_dir( $source ) ) { @mkdir( $destination ); $directory = dir( $source ); while ( FALSE !== ( $readdirectory = $directory->read() ) ) { if ( $readdirectory == '.' || $readdirectory == '..' ) { continue; } $PathDir = $source . '/' . $readdirectory; if ( is_dir( $PathDir ) ) { copy_directory( $PathDir, $destination . '/' . $readdirectory ); continue; } copy( $PathDir, $destination . '/' . $readdirectory ); } $directory->close(); } else { copy( $source, $destination ); } } ?>

2. Usage Copy Entire Directory Function

<?php
	copy_directory("D:/dev/web/htdocs/4rapiddev/database", "D:/dev/web/htdocs/4rapiddev/backup/database");
	copy_directory("D:/dev/web/htdocs/4rapiddev/logs", "D:/dev/web/htdocs/4rapiddev/backup/logs");
?>

<?php copy_directory("D:/dev/web/htdocs/4rapiddev/database", "D:/dev/web/htdocs/4rapiddev/backup/database"); copy_directory("D:/dev/web/htdocs/4rapiddev/logs", "D:/dev/web/htdocs/4rapiddev/backup/logs"); ?>

Note:

  • 1. The destination directory/folder must have write permission or in a writable directory
  • 2. After copy the directory to another place, we may need to completely delete this in order to save our web space.
Mar 4, 2012Hoan Huynh
Facebook Timeline And New Design For PagesC# How to Get key/value pairs from app.config and web.config
You Might Also Like:
  • PHP Completely Delete A Directory Include Sub Directories And Files
  • Linux copy entire directory
  • Linux Zip Entire Directory Recursively
  • PHP Delete File Function
  • PHP check if folder exists then create folder
  • Free, Simple And Easiest Copy Software
  • Linux remove entire directory
  • HTTP/ HTTPS Document, CGI-BIN And Access/ Error Logs On cPanel, Plesk And VirtualMin
  • Fix SoftException in Application.cpp Directory/File is writeable by group
  • Linux Chmod Change Mode Directory Recursive
Hoan Huynh

Hoan Huynh is the founder and head of 4rapiddev.com. Reach him at [email protected]

9 years ago PHPdir, is_dir, mkdir669
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,162 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,040 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,821 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,296 views
C# Read Json From URL And Parse/Deserialize Json
9,790 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Installment Loans – Making Sense of Online Software
  • Apple Pay Casino Canada
  • Casinos austria
  • Essay For Sale – How To Write A Superb One That Will Sell
  • Free Online Photo Editor
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (103)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development