Solved

copying files from one folder to another folder

Posted on 2008-10-01
10
872 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 108

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 108

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
 
LVL 108

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 108

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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 108

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 108

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now