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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.