Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

copy folder A and its contents to another folder B

Posted on 2008-10-06
3
Medium Priority
?
269 Views
Last Modified: 2012-05-05
i have a snippet of code that i need some help with.  i want to copy folder A "core" and all its contents to folder B "<customer_name>".

so far my functions just copy files.  it doesnt copy any subfolder files.

/public_html/xxx/cms/core/  <----------------- folder A is core.  core contains 3 txt files and a sub folder called "includes" with 4 txt files .

/<customer_name>/public/xxx/  <-------------------- folder B is <customer_name> - the param i pass into my SetupCustomer function.

basically im copying everything inside core to a folder outside the public_html

see code below:
function CopyCoreFiles($current_directory,$template_directory,$customer_directory)
{
	$dots = array('.', '..');
	$files = array_diff(scandir($template_directory), $dots);
			
	foreach($files as $file) 
	{
		chdir($template_directory);
 
		$myfile = file_get_contents($file);
		
		chdir($current_directory);	
		
		chdir($customer_directory);
		
		file_put_contents($file,$myfile);	
		
		chdir($current_directory);
	}
		
	$files = array_diff(scandir($customer_directory), $dots);
		
	if(count($files) > 0)
	{
		unset($files,$file,$dots,$myfile,$customer_directory,$template_directory,$current_directory);
			
		return 1;
	}
	else
	{
		unset($files,$file,$dots,$myfile,$customer_directory,$template_directory,$current_directory);
			
		return 0;
	}
}
 
function SetupCustomer($customer_name)
{
	$current_directory = '';
		
	if(is_dir($customer_name))
	{			
		$this->DeleteFolderAndFiles($customer_name.'/');
	
		rmdir($customer_name);
	}	
		
	mkdir($customer_name);
		
	$current_directory = getcwd();
		
	if($this->CopyCoreFiles($current_directory,'cms/core/',$customer_name) == 1)
	{	
		unset($current_directory,$customer_name,$handle,$file,$current_directory);
			
		return true;
	}
	else
	{
		unset($current_directory,$customer_name,$handle,$file,$current_directory);
			
		return false;
	}
}
 
SetupCustomer('pegasus');

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
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
deresh earned 2000 total points
ID: 22653507
you need to extend your function to include "includes" folder like this:
function CopyCoreFiles($current_directory,$template_directory,$customer_directory)
{
        $dots = array('.', '..');
        $files = array_diff(scandir($template_directory), $dots);
                        
        foreach($files as $file) 
        {
                chdir($template_directory);
                $myfile = file_get_contents($file);
                chdir($current_directory);      
                chdir($customer_directory);
                file_put_contents($file,$myfile);       
                chdir($current_directory);
        }
 
        mkdir($customer_directory."/includes");
 
        $dots = array('.', '..');
        $files = array_diff(scandir($template_directory."/includes"), $dots);
                        
        foreach($files as $file) 
        {
                chdir($template_directory);
                $myfile = file_get_contents($file);
                chdir($current_directory);      
                chdir($customer_directory."/includes");
                file_put_contents($file,$myfile);       
                chdir($current_directory);
        }
 
                
        $files = array_diff(scandir($customer_directory), $dots);
                
        if(count($files) > 0)
        {
                unset($files,$file,$dots,$myfile,$customer_directory,$template_directory,$current_directory);
                        
                return 1;
        }
        else
        {
                unset($files,$file,$dots,$myfile,$customer_directory,$template_directory,$current_directory);
                        
                return 0;
        }

Open in new window

0
 
LVL 16

Author Comment

by:ellandrd
ID: 22653560
Hi

Looking at your code without testing it yet, will it copy all files inside core and its sub folder includes?
0
 
LVL 6

Expert Comment

by:deresh
ID: 22654079
yes, it will. but you should do additional checks to be 100% sure, as i don't do any checkings ( if includes folder exists etc..)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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 i…
The viewer will learn how to dynamically set the form action using jQuery.
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.
Suggested Courses

598 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