• Copy Files from one folder to another folder using PHP

    To copy files from source folder to destination php provide a copy function. This function takes two argument first is source path (source dir) and destination path (destination dir) and copy files from source to destination path.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    
    <?php
     
    copyimages($dir,$dirNew); //$dir -  source path, $dirNew – destination path 
     
    //copy files to folder
     
    function copyimages($dir,$dirNew){
     
    // Open a known directory, and proceed to read its contents
     
        if (is_dir($dir)) {
     
            if ($dh = opendir($dir)) {
     
                while (($file = readdir($dh)) !== false) {
     
                //exclude unwanted 
     
                if ($file==".") continue;
     
                if ($file=="..")continue;
     
                //if ($file=="index.php") continue; for example if you have index.php in the folder
     
                copy("$dir/$file","$dirNew/$file");
     
                }
     
                closedir($dh);
     
            }

Comments on this post

Leave a Reply

  • Security Code :


    2 + five =