Solved

copying files from one folder to another folder

Posted on 2008-10-01
10
877 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:ellandrd
  • 6
  • 4
10 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22618565
When you say "it doesnt really work as well as i thought." -- what do you mean?  What is not working?
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22618725
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?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 22619034
/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


0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22619226
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?
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 22619525
You might be able to do it with something like this... But I'll wait till I see ALL the code you're using. ~Ray
<?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);
 
	foreach ($template_files as $tf) {
		chdir($template_directory);
		$my_file = file_get_contents($tf);
		chdir($old_directory);
		chdir($customer_directory);
		file_put_contents($tf);
	}
}
?>

Open in new window

0
 
LVL 16

Author Comment

by:ellandrd
ID: 22621990
my page has over 1400 lines of code - im not posting ALL the code here.
0
 
LVL 16

Author Comment

by:ellandrd
ID: 22622655
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

0
 
LVL 16

Author Comment

by:ellandrd
ID: 22623170
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

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22623192
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
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 22623225
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
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question