Solved

copying files from one folder to another folder

Posted on 2008-10-01
10
883 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 110

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 110

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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 110

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 110

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 110

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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
if (is_singular not working 5 39
How to use session variables in php? 22 51
PHP Underscores vs. Directory separators 5 29
php hashing methods 3 16
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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