Change language

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

Functions used:
  • Function copy(): The copy() function is used to create a copy of the specified file. It makes a copy of the source file to the destination file, and if the destination file already exists, it is overwritten. Copy() returns true on success and false on error. Syntax:
    bool copy ($source, $dest)
  • opendir() function :opendir() function is used to open a handle to a directory. The path to the directory to open is sent as a parameter to the opendir() function, and it returns the directory handle resource on success or FALSE on error. Syntax:
    opendir ($path, $context)
  • is_dir() function : function is_dir() is used to check if the specified file is a directory or not. The filename is sent as a parameter to the is_dir() function and returns True if the file is a directory, otherwise it returns False. Syntax:
    is_dir ($file)
  • scandir() function : function scandir() is used to return an array of files and directories of the specified directory. The scandir() function lists the files and directories that are present in the specified path. The directory, stream behavior, and sorting_order of files and directories are passed as a parameter to the scandir() function, and it returns an array of filenames on success or False on error. Syntax:
     scandir (directory, sorting_order, context)
  • readdir() function: readdir() is used to return the name of the next entry in a directory. This method returns the file names in the order in which they are stored in the file naming system. The directory handle is passed as a parameter to the readdir() function and returns the entry name / filename on success or False on error. Syntax:
    readdir (dir_handle)
Example 1:This example uses readdir() to read files from a source directory. function custom_copy ( $src , $dst ) { // open source directory $dir = open dir ( $src ); // Make the destination directory if it doesn’t exist @ mkdir ( $dst ); // Loop through the files in the source directory while ( $file = readdir ( $dir )) { if (( $file ! = ’.’ ) & amp; & amp; ( $file ! = ’ ..’ )) { if ( is_dir ( $src . ’/’ . $file )) { // Recursively call a custom copy function // for subdirectory custom_copy ( $src . ’ / ’ . $file , $dst . ’/’ . $file ); } else { copy ( $src . ’/’ . $file , $dst . ’ / ’ . $file ); } } } closedir ( $dir ); } $src = "C: / xampp / htdocs / engineer" ; $dst = " C: / xampp / htdocs / gfg " ; custom_copy ( $src , $dst );  
?>
Output:
  • Before starting the program on the local host:
  • After starting the program on the local host:
Example 2:This example uses the scandir() function to read files from the source directory. function custom_copy ( $src , $dst ) {   // open source directory $dir = opendir ( $src ); // Make the destination directory if it doesn’t exist @ mkdir ( $dst ); // Loop through the files in the source directory foreach (scandir ( $src ) as $file ) {   if (( $file ! = ’.’ ) & amp; & amp; ( $file ! = ’..’ )) { if ( is_dir ( $src . ’/’ . $file )) { // Recursively call a custom copy function // for subdirectory custom_copy ( $src . ’ / ’ . $file , $dst . ’/’ . $file ); } else { copy ( $src . ’/’ . $file , $dst . ’ / ’ . $file ); } } } closedir ( $dir ); } $src = "C: / xampp / htdocs / engineer" ; $dst = " C: / xampp / htdocs / gfg " ; custom_copy ( $src , $dst );  
?>
Output:
  • Before starting the program on the local host:
  • After starting the program on the local host:

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method