Friday, October 24, 2025
HomeLanguagesCopy the entire contents of a directory to another directory in PHP

Copy the entire contents of a directory to another directory in PHP

Given a directory and the task is to copy the content of the directory to another directory using PHP functions. There are many functions used to copy the content of one directory to another.

Used Functions:

  • copy() Function: The copy() function is used to make a copy of a specified file. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure.

    Syntax:

    bool copy($source, $dest)
  • opendir() Function: The opendir() function is used to open a directory handle. The path of the directory to be opened is sent as a parameter to the opendir() function and it returns a directory handle resource on success, or FALSE on failure.

    Syntax:

    opendir($path, $context)
  • is_dir() Function: The is_dir() function is used to check whether the specified file is a directory or not. The name of the file is sent as a parameter to the is_dir() function and it returns True if the file is a directory else returns False.

    Syntax:

    is_dir($file)
  • scandir( ) Function: The scandir() function is used to return an array of files and directories of the specified directory. The scandir() function lists the files and directories which are present inside a specified path. The directory, stream behavior and sorting_order of the files and directories are passed as a parameter to the scandir() function and it returns an array of filenames on success, or False on failure.

    Syntax:

    scandir(directory, sorting_order, context)
  • readdir() Function: The readdir() function is used to return the name of the next entry in a directory. This method returns the filenames in the order as they are stored in the filenamesystem. The directory handle is sent as a parameter to the readdir() function and it returns the entry name/filename on success or False on failure.

    Syntax:

    readdir(dir_handle)

Example 1: This example uses readdir() function to read files from the source directory.




<?php
  
function custom_copy($src, $dst) { 
  
    // open the source directory
    $dir = opendir($src); 
  
    // Make the destination directory if not exist
    @mkdir($dst); 
  
    // Loop through the files in source directory
    while( $file = readdir($dir) ) { 
  
        if (( $file != '.' ) && ( $file != '..' )) { 
            if ( is_dir($src . '/' . $file) ) 
            { 
  
                // Recursively calling custom copy function
                // for sub directory 
                custom_copy($src . '/' . $file, $dst . '/' . $file); 
  
            } 
            else { 
                copy($src . '/' . $file, $dst . '/' . $file); 
            } 
        } 
    } 
  
    closedir($dir);
} 
  
$src = "C:/xampp/htdocs/neveropen";
  
$dst = "C:/xampp/htdocs/gfg";
  
custom_copy($src, $dst);
  
?>


Output:

  • Before Run the program on localhost:
  • After Run the program on localhost:

Example 2: This example uses scandir() function to read files from the source directory.




<?php
    
function custom_copy($src, $dst) { 
   
    // open the source directory
    $dir = opendir($src); 
   
    // Make the destination directory if not exist
    @mkdir($dst); 
   
    // Loop through the files in source directory
    foreach (scandir($src) as $file) { 
   
        if (( $file != '.' ) && ( $file != '..' )) { 
            if ( is_dir($src . '/' . $file) ) 
            { 
   
                // Recursively calling custom copy function
                // for sub directory 
                custom_copy($src . '/' . $file, $dst . '/' . $file); 
   
            } 
            else { 
                copy($src . '/' . $file, $dst . '/' . $file); 
            } 
        } 
    } 
   
    closedir($dir);
}  
  
$src = "C:/xampp/htdocs/neveropen";
  
$dst = "C:/xampp/htdocs/gfg";
  
custom_copy($src, $dst);
  
?>


Output:

  • Before Run the program on localhost:
  • After Run the program on localhost:
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS