How to copy files from one directory and adding two supporting files into the copied directory/directories



  • Continuing from the previous question solved "How do I finish writing in a delete function into this code to delete both directory and files in it?"
    We have directories moved daily to a destination folder entitled "archive" here we can now delete unneeded images from those moved directories.
    But would like to be able to add two supporting files when moving into the directory being moved.
    As below..for example 20200402 and 20200404 are the moved directories, in both reside an image folder and will always have a different name. File1 and file2 would be copied from another supporting file directory and placed in 20200402 and 200200404.
    My guess is that it would make more sense to add the supporting files before they are moved, not sure how to do that.

    file1-2.jpg
    Below is the script that does the move work and excludes specific files moving what is necessary.

    <? php    
        **$dir = ".";** //"path/to/targetFiles";
        **$dirNew = "archive";** //path/to/destination/files
        // Open a known directory, and proceed to read its contents
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                echo '<br>Archive: '.$file;
                //exclude unwanted 
                if ($file=="move.php")continue;
                if ($file==".") continue;
                if ($file=="..")continue;
                if ($file=="archive")continue;
                if (rename($dir.'/'.$file,$dirNew.'/'.$file))
                    {
                    echo " Files Copyed Successfully";
                    echo ": $dirNew/$file"; 
                     }
                    else {echo "File Not Copy";}
                }
                closedir($dh);
            }
        }
        ?>
    


  • How about something like this:

         $dir = "."; //"path/to/targetFiles";
        $dirNew = "archive"; //path/to/destination/files
        // =================== ADD THIS =========================== 
        $from_dir = "files"; // CHANGE THIS 
        $file_name_1 = "extra_file_1.png"; // CHANGE THIS 
        $file_name_2 = "extra_file_2.png"; // CHANGE THIS 
        
        // Open a known directory, and proceed to read its contents
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                echo '<br>Archive: '.$file;
                //exclude unwanted
                if ($file=="move.php")continue;
                if ($file==".") continue;
                if ($file=="..")continue;
                if ($file=="archive")continue;
                // =================== ADD THIS =========================== 
                if ($file==$from_dir)continue;
    
               if (rename($dir.'/'.$file,$dirNew.'/'.$file))
                    {
                    echo " Files Copied Successfully";
                    echo ": $dirNew/$file";
                    // =================== ADD THIS =========================== 
                    copy($from_dir.'/'.$file_name_1, $dirNew.'/'.$file.'/'.$file_name_1);
                    copy($from_dir.'/'.$file_name_2, $dirNew.'/'.$file.'/'.$file_name_2);
                    
                     }
                    else {echo "File Not Copy";}
                }
                closedir($dh);
            }
        }
    

    This will move two files located in the folder "files" into each destination folder.



  • Hey @MPG-Radio

    I would love to help but I am still a little confused. What exactly are you trying to achieve. You want to move extra files/folder into a destination folder that you want to exclude from a delete process later?



  • Sort of.
    I need to add two files when moving a folder. the folder that is moved has one subfolder with images. Everything gets moved but I need to also add two supporting files with it.



  • How about something like this:

         $dir = "."; //"path/to/targetFiles";
        $dirNew = "archive"; //path/to/destination/files
        // =================== ADD THIS =========================== 
        $from_dir = "files"; // CHANGE THIS 
        $file_name_1 = "extra_file_1.png"; // CHANGE THIS 
        $file_name_2 = "extra_file_2.png"; // CHANGE THIS 
        
        // Open a known directory, and proceed to read its contents
        if (is_dir($dir)) {
            if ($dh = opendir($dir)) {
                while (($file = readdir($dh)) !== false) {
                echo '<br>Archive: '.$file;
                //exclude unwanted
                if ($file=="move.php")continue;
                if ($file==".") continue;
                if ($file=="..")continue;
                if ($file=="archive")continue;
                // =================== ADD THIS =========================== 
                if ($file==$from_dir)continue;
    
               if (rename($dir.'/'.$file,$dirNew.'/'.$file))
                    {
                    echo " Files Copied Successfully";
                    echo ": $dirNew/$file";
                    // =================== ADD THIS =========================== 
                    copy($from_dir.'/'.$file_name_1, $dirNew.'/'.$file.'/'.$file_name_1);
                    copy($from_dir.'/'.$file_name_2, $dirNew.'/'.$file.'/'.$file_name_2);
                    
                     }
                    else {echo "File Not Copy";}
                }
                closedir($dh);
            }
        }
    

    This will move two files located in the folder "files" into each destination folder.



  • Thank you, so now I know how to include more than one file and not just the files in a directory when moving a directory to another directory.


Log in to reply