Jump to content

copy recursive files, ignore folders?!


kev@num

Recommended Posts

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;
}
?>

 

:)

 

Link to comment
https://forums.phpfreaks.com/topic/205846-copy-recursive-files-ignore-folders/
Share on other sites

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;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.