johnc71 Posted April 29, 2009 Share Posted April 29, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/156079-copying-a-single-file-to-multiple-sub-directories/ Share on other sites More sharing options...
ignace Posted April 29, 2009 Share Posted April 29, 2009 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'); } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156079-copying-a-single-file-to-multiple-sub-directories/#findComment-821692 Share on other sites More sharing options...
johnc71 Posted April 29, 2009 Author Share Posted April 29, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/156079-copying-a-single-file-to-multiple-sub-directories/#findComment-822152 Share on other sites More sharing options...
Daniel0 Posted April 29, 2009 Share Posted April 29, 2009 Sorry, I did not mention that the PHP version is 4. Upgrade. No excuses. Quote Link to comment https://forums.phpfreaks.com/topic/156079-copying-a-single-file-to-multiple-sub-directories/#findComment-822153 Share on other sites More sharing options...
Ken2k7 Posted April 29, 2009 Share Posted April 29, 2009 LOL @ Daniel0's response. Yeah, you really should upgrade. Anyways, you can change the permissions temporarily. Look into fileperms() and chmod(). Quote Link to comment https://forums.phpfreaks.com/topic/156079-copying-a-single-file-to-multiple-sub-directories/#findComment-822157 Share on other sites More sharing options...
johnc71 Posted April 30, 2009 Author Share Posted April 30, 2009 Ok... The file is copied but it ends up with 0 bytes. What gives? Quote Link to comment https://forums.phpfreaks.com/topic/156079-copying-a-single-file-to-multiple-sub-directories/#findComment-822478 Share on other sites More sharing options...
ignace Posted April 30, 2009 Share Posted April 30, 2009 Ok... The file is copied but it ends up with 0 bytes. What gives? No write permission? Quote Link to comment https://forums.phpfreaks.com/topic/156079-copying-a-single-file-to-multiple-sub-directories/#findComment-822500 Share on other sites More sharing options...
johnc71 Posted April 30, 2009 Author Share Posted April 30, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/156079-copying-a-single-file-to-multiple-sub-directories/#findComment-823071 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.