Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag for Ireland

asked on

copying files from one folder to another folder

i have setup a script to copy all files from one folder to another folder but it doesnt really work as well as i thought.  maybe somebody can have a look please.

source folder is:  /public_html/xxx/cms/core/
destination folder is: /public_html/xxx/<customer_name>/

in the "core" folder i have 3 files that i want to copy into the "customer_name"

my code snippet below exists in a file called setup.php that exists under /public_html/xxx/setup.php

p.s the function SetupShop() is called first...

please see code below:
function SetupShop($customer_name) {
	if(is_dir($customer_name)) {			
		$this->DeleteFolderAndFiles($customer_name.'/');
		rmdir($customer_name);
	}	
		
	mkdir($customer_name,0755);
	chmod($domain,0777);
		
	if($this->CopyCoreFiles('cms/core/',$customer_name) > 0) {
		unset($domain,$handle,$file);	
		return true;
	} else {	
		unset($domain,$handle,$file);
		return false;
	}
}
 
function CopyCoreFiles($source,$destination) {
	$dir = $file = $srcfile = $dstfile = $ow = '';
	$num = 0;
 
	if($dir = opendir($source)) {
		while(false !== ($file = readdir($dir))) {
			if($file != '.' && $file != '..') {
				$srcfile = $source . '/' . $file;
				$dstfile = $destination . '/' . $file;
 
				if(is_file($srcfile)) {
					if(is_file($dstfile)) { 
						$ow = filemtime($srcfile) - filemtime($dstfile); 
					} else {	
						$ow = 1;
					}
 
					if($ow > 0) {	
						if(copy($srcfile,$dstfile)) {
							touch($dstfile,filemtime($srcfile)); 
							$num++;							
						}
					}                  
				} elseif(is_dir($srcfile)) {
					$num += $this->CopyCoreFiles($srcfile,$dstfile);
				}
			}
		}
 
		closedir($dir);
	}
 
	unset($dir,$ow,$source,$destination,$srcfile,$dstfile,$file);
        
        return $num;
}
 
function DeleteFolderAndFiles($directory) {
	if($handle = opendir($directory))  {
		// check if we can access the folder
		while(false !== ($file = readdir($handle))) {
			// check if sub folders esist
			if($file != '.' && $file != '..') {	
				// delete the file
				unlink($directory.$file);
			}
		}
			
		// lets close the folder handler
		closedir($handle);
	}
		
	return true;
}

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

When you say "it doesnt really work as well as i thought." -- what do you mean?  What is not working?
Is this part of a class?  Where is $domain defined?  It looks like there is more to this code that you have not posted here... Hmm.

In  /public_html/xxx/<customer_name>/ will there ever be any directories?  Or will that hold only files?
Avatar of ellandrd

ASKER

/public_html/xxx/<customer_name>/  will only contain other PHP files and a txt file

ok i renamed my $domain variable to $customer_name.  forgot to up my code!  :-(

ok i have tried my code again with updated variable and it still doesnt work.

i dont get any errors and my 3 files from core isnt copied into subfolder $customer_name


Thanks.  Please use the code snippet and post ALL the code, from <?php to ?>

I'll see if I can fix it up.  What level of PHP are you running?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
my page has over 1400 lines of code - im not posting ALL the code here.
within the foreach loop, the CWD doesnt change to $template_directory at line 28 in your code snippet above

however if i print out the files in the $template_directory i get my 3 files

got it working now.... few changes but its copying the files... however i did notice it doesnt copy a subfolder if i had one inside the cms/core/ folder


<?php
function make_new_customer($customer_directory) 
{
	// SAVE THE CURRENT DIRECTORY
	$old_directory = getcwd();
	
	// REMOVE THE OLD DIRECTORY
	$useless_dots = array('.', '..');
	$customer_files = array_diff(scandir($customer_directory), $useless_dots);
	
	chdir($customer_directory);
 
	foreach ($customer_files as $cf) 
	{
		unlink($cf);
	}
 
	chdir($old_directory);
	rmdir($customer_directory);
 
	// MAKE THE NEW DIRECTORY
	mkdir($customer_directory);
 
	// COPY THE TEMPLATE FILES TO THE CUSTOMER
	$template_directory = 'cms/core/';
	$useless_dots = array('.', '..');
	$template_files = array_diff(scandir($template_directory), $useless_dots);
 
	chdir($old_directory);
	
	echo 'CWD 1: '.getcwd().'<br>';
			
	foreach ($template_files as $tf) 
	{
		chdir($template_directory);
		
		echo 'CWD 2: '.getcwd().'<br>';
		
		echo $tf.'<br>';
		
		$my_file = file_get_contents($tf);
		
		chdir($old_directory);	
		
		echo 'CWD 3: '.getcwd().'<br>';
		
		chdir($customer_directory);
		
		echo 'CWD 4: '.getcwd().'<br>';
		
		file_put_contents($tf,$my_file);
		
		chdir($old_directory);
		
		echo 'CWD 5: '.getcwd().'<br>';
	}
}
 
make_new_customer('pegasus');
?>

Open in new window

When you say "the CWD doesnt change to $template_directory at line 28" - what do you mean?  What is the symptom or the output?  Thanks.

Also, posting 1400 lines of code is not too much for EE to handle, so don't be bashful.  What I am seeking to avoid is an exercise that includes debugging program code that is not actually the program code you are using - like the place above where a variable name got changed in the program, but the code snippet did not reflect the change.  There are a lot of automated tools that can catch such errors, but they are rendered useless if they are put to work on the wrong source code.  If you have a segment that is in a class or function that you load with a require_once() or equivalent, that segment is probably enough (so long as we can also find any external variables).

Best regards, ~Ray
Ahh, our messages seem to have passed in cyberspace.

No, it will not copy directories - it will copy files.  Relying on this in your original post:

'in the "core" folder i have 3 files that i want to copy into the "customer_name"'

Copying directory trees is really a separate question.  Anyway, I'm glad you have it working.  Please let me know if you have any questions or need further help.  ~Ray