Jump to content

Copying a single file to multiple sub-directories


johnc71

Recommended Posts

Can this be done?

 

I want to copy a single file "index.html" to any sub-directory in the current location that begins with "demo".

 

Of course I can do this by running the code below, but how can I do this by using LOOP.

 

so it would be something like this

 

Go thru all sub-directories

for each sub-directory where the first four characters of sub-directory == 'demo')

{

copy index.html to that sub-directory

}

 

This is the current non-practical code I have

$file = 'index.html';
$dest1 = 'demo/index.html';
$dest2 = 'demoABC/index.html';
$dest3 = 'demo123/index.html';

copy($file, $dest1);
copy($file, $dest2);
copy($file, $dest3);

 

Link to comment
Share on other sites

You had a beautiful pseudo-code example, why not implement it?

 

<?php
$indexHtml = 'path/to/index/html/file';
$directoryListing = scandir('path/to/directory');
foreach ($directoryListing as $directory) {
   if (is_dir($directory) && is_writeable($directory)) {
       $directoryName = basename($directory);
       if (substr($directoryName, 0, 4) === 'demo') {
          copy($indexHtml, $directory .DIRECTORY_SEPARATOR. 'index.html');
       }
   }
}
?>

Link to comment
Share on other sites

Sorry, I did not mention that the PHP version is 4.  I modified your code to make it compatible with 4.0 and although it may not be the cleanest approach, it works. 

 

Now the challenge I have is, if the folder name matches but the folder is not writable, how do I get around it?  Can I temporarily change the permisions on that folder either via chown or chmod  to make it writable and than restore the original permissions?

 

Here is the current code:

<?php

$indexHtml = 'index.html';
$current_directory = "/usr/local/apache/htdocs/";
$dir_contents = opendir($current_directory);
while ($sub_directory = readdir($dir_contents)){
  if($sub_directory != "." && $sub_directory != ".."){
 	if (is_dir($current_directory."/".$sub_directory) && is_writeable($current_directory."/".$sub_directory)){ 
    	$directoryName = basename($sub_directory);
       		if(substr($directoryName, 0, 4) === 'demo'){
          	  copy($indexHtml, $sub_directory .DIRECTORY_SEPARATOR. 'index.html');
	      }
      } 
     }
}
closedir($dir_contents);

?>

Link to comment
Share on other sites

Ok, I resolved the issue with zero bytes files.  So, the final problem I have now is with target directory permisions.

 

If I want to copy file A to folder B, I can't because the folder B rights are set to 755.   

 

I tried to chmod the folder B to 777 before copying file, but since the php file is in group "www" I do not have right to chmod folder B. 

 

Arghhh... 

 

If I ssh to the server and change folder owner to www, am I compromising the security of that folder? 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.