PHP – Find and copy files in dir and sub dir

I recently came across some small torrent with some terrible structure and I really wanted to have all the files in a single folder but I am way too lazy to do it by hand, so I wrote this small “program” to do it for me.

The main function that I use for this task is glob which find pathnames matching some pattern, from there is a really straight forward application and easy to follow.

How to use: just call copyfiles('*.epub', 'allbooks'); function passing the pattern you want to find and the destination folder, if the folder does not exist it will be created and if a file is duplicated it will be overwritten.

Why PHP? hmmm well it was the file that I had open… maybe other language would had look better – python or C# could have been a better option..

For more info:
glob function –> http://php.net/manual/en/function.glob.php
copy function –> http://php.net/manual/en/function.copy.php
mkdir function –> http://php.net/manual/en/function.mkdir.php

 
 
 

Leave a Reply

Your email address will not be published.