So a co-worker needed to find a way of coping a few files (10 files) to a bunch of folders if the files weren’t already present. Instead of doing a manual copy paste in Windows I created this code to do the above. If anyone needs to do something similar feel free to use this code. Pretty much all is does is create an array with all the folders where this PHP file is being run (this is the root where the bunch of folders reside). Then it creates another array of files – these are the files that we need to copy to all the folders. This array is created by looking in the templates folder and adding each file to the array.
The ‘foldersOnly’ function gets the array from scandir() and cleans it up. It removes the ‘.’, ‘..’, and a couple Dreamweaver folders like ‘_notes’ and another one I can remember. The new array returns only the folders we want to work with. The ‘filesOnly’ gets the array from the PHP function scandir() and returns only the files in that folder. Once we have our arrays we are ready to start.
We are going to loop for each folder and inside each folder look for each file. We check to see if the file is in the folder, if so we do nothing. If the file is not there, we copy it from the templates folder. This is pretty much it, once all the loops are done every folder should have the missing files. The code is below: Continue reading “PHP – Copying x Files to n Folders”