kev@num Posted June 25, 2010 Share Posted June 25, 2010 Hi, i've had a google, and search but can't find an answer. What I want to do is copy all files from within a folder structure to one directory and not copy the folders - so all files deep within folders appear in one destination folder - no filenames will be the same, so no need to worry about overwriting - no folders are needed in the dest, just the files. I have found the following function for copying files and folders recursively but wondered if I could just comment out a little to get it to do what I want? or if there's an easier way? Thanks in advance, any help would be much appreciated! <?php function COPY_RECURSIVE_DIRS($dirsource, $dirdest){ if(is_dir($dirsource))$dir_handle=opendir($dirsource); mkdir($dirdest."/".$dirsource, 0750); while($file=readdir($dir_handle)){ if($file!="." && $file!=".."){ if(!is_dir($dirsource."/".$file)) copy ($dirsource."/".$file, $dirdest."/".$dirsource."/".$file); else COPY_RECURSIVE_DIRS($dirsource."/".$file, $dirdest); } } closedir($dir_handle); return true; } ?> Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted June 25, 2010 Share Posted June 25, 2010 Oops, didn't really look at the code. Here is a revised version of your code, cleaned it up a tad as well. Basically, removed the call to mkdir. <?php function COPY_RECURSIVE_DIRS($directory, $destination){ if(is_dir($directory)){ $handle = opendir($directory); } while(($file = readdir($handle)) !== false){ if($file != "." && $file != ".."){ if(!is_dir($directory . "/" . $file)){ copy($directory . "/" . $file, $destination . $file); }else{ COPY_RECURSIVE_DIRS($directory . "/" . $file, $destination); } } } closedir($handle); return true; } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.