?
Solved

copying files from one folder to another folder

Posted on 2008-10-01
10
Medium Priority
?
896 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
10 Comments
 
LVL 111

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 111

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
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 
LVL 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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 111

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …
Suggested Courses

764 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