Cakephp copy whole app directory and subdirectories? - php

Hi I'm a new born programmer, trying to build a Cakephp System that allow user to register then create a directory for them and copy a prebuild system of mine to their directory for free. But I'm stack at copy my app directory to their directory. I have try to use recursive copy function but it only copy the file not all subdirectory.
Please help me with this one.
Edit:
Sorry for didn't attach the code
Here the code:
function recurse_copy($src,$dst) {
$dir = opendir($src);
#mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}

Try something like this:
<?php
foreach (
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST) as $item
) {
if ($item->isDir()) {
mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
} else {
copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
}
?>
If you want your code snippet to be looked over please post it

Related

Php beginners questions, how can I exclude a folder in this script

can somebody help me? I use below script and it works but I want to exclude folders. If there are any better scripts out there that's also fine, I tried a lot but in combination with magento its a bit hard for me to understand. So the goal is to copy a folder to another folder including symlinks and everying inside the folders. Problem with this script is that he copies inside the same folder and then creates a loop
$source = Mage::getBaseDir();
$dest = Mage::getStoreConfig('Setupstaging_options/product_page/stagingfolder');
mkdir($dest, 0755);
foreach (
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST) as $item) {
if ($item->isDir()) {
mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
} else {
copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
}
If you have the name of the folders you want to skip:
$exclude_dirs = array("onedir", "twodir"); // List of directory names to exclude
$source = Mage::getBaseDir();
$dest = Mage::getStoreConfig('Setupstaging_options/product_page/stagingfolder');
mkdir($dest, 0755);
foreach ($iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST) as $item) {
if ($item->isDir() && !in_array($item->getFilename(), $exclude_dirs)) { // If this Dir's name is not in $exclude_dirs
mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
else {
copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
}
I think that you can use this condition :
if ($item->isDir()) {
if($item->getSubPathName() =! "your path directory to exclude"){
mkdir($dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}
} else {
copy($item, $dest . DIRECTORY_SEPARATOR . $iterator->getSubPathName());
}

PHP: Copy entire contents of a directory

Sorry for new post! I'm not yet trusted to comment on others posts.
I'm having trouble copying folders and this is where I started:
Copy entire contents of a directory
Function
function recurse_copy($src,$dst) {
$dir = opendir($src);
#mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
My input
$src = "http://$_SERVER[HTTP_HOST]/_template/function/";
$dst = "http://$_SERVER[HTTP_HOST]/city/department/function/";
recurse_copy($src, $dst);
I've also tried this
$src = "$_SERVER[DOCUMENT_ROOT]/_template/function/"; // And so on...
The function is executed but nothing is being copied.
Any ideas on what might be wrong?
SOLVED
Working solution
Along with
$src = "$_SERVER[DOCUMENT_ROOT]/_template/function/";
$dst = "$_SERVER[DOCUMENT_ROOT]/city/department/function/";
recurse_copy($src, $dst);
It's not tested but I think the issue might be that the target directory is not necessarily being created before attempting to copy files to it. The piece of code that creates the target directory would require a folder path rather than a full filepath - hence using dirname( $dst )
if( !defined('DS') ) define( 'DS', DIRECTORY_SEPARATOR );
function recurse_copy( $src, $dst ) {
$dir = opendir( $src );
#mkdir( dirname( $dst ) );
while( false !== ( $file = readdir( $dir ) ) ) {
if( $file != '.' && $file != '..' ) {
if( is_dir( $src . DS . $file ) ) {
recurse_copy( $src . DS . $file, $dst . DS . $file );
} else {
copy( $src . DS . $file, $dst . DS . $file );
}
}
}
closedir( $dir );
}
Use local paths
$src= "_template/function/";
$dst= "city/department/function/";
recurse_copy($src, $dst);
copy works locally on your server. You're trying to copy using HTTP scheme, it's not working that way.

How to delete all folders and files in a directory except a specific folder in PHP

How can I delete all folders and files except a specific folder?
uploaded->
. folder_A->
. . folder_A1 //empty folder
. . folder_A2 //full folder
. . img.png // a file
. .
. folder_B //empty
. .
. folder_c->
. . folder_c1 //empty folder
. . file.doc // a file
. .
I want remove all folders and file in it inside "uploaded" folder except a specific folder that I determined.
For example I want remove all folders and files except folder_c
you should try like this
function Delete($path)
{
if ((is_dir($path) === true) && ($path!='folder_c'))
{
$files = array_diff(scandir($path), array('.', '..'));
foreach ($files as $file)
{
Delete(realpath($path) . '/' . $file);
}
return rmdir($path);
}
else if (is_file($path) === true)
{
return unlink($path);
}
return false;
}
Hope this helps.

Copy contents of a directory to lots of other subdirectories

I have a some template files organised like this in a directory:
index.php
preferences.xml
user.xml
I have also have subdirectory of this directory that contains a large number of folders (the exact number of folders can change a lot) which have outdated versions of the above template files:
randomfolder1
randomfolder20
randomfolder29
...
I would like to be able to copy the template files into all of these folders in the subdirectory, regardless of how many directories or template files there are. This is an example of what I would like in every folder:
randomfolder
- index.php
- preferences.xml
- user.xml
I have used this to copy the template files into ONE directory, but not all of them:
$cdire = 'GMXD/Users/firstdirectory';
mkdir($cdire);
function recurse_copy($src,$dst) {
$dir = opendir($src);
#mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
recurse_copy('template',$cdire);
So how can you make this script copy all files in the template directory to all files in the subdirectory ?

Copying files from multiple source to destination directories using PHP recursive copy function

The purpose of this question can be served by writing independent function for each source & destination directory in an include file but I'm looking for a better approach.
The following function copy files from one source directory to one destination directory.
How can I use this function to copy file from another source directory to destination directory?
Is array(); applicable here or explode(); shall be the right choice or none of these is applicable in this case?
if (isset($_POST['submit'])) {
$old_umask = umask(0);
if (!is_dir($dst)) mkdir($dst, 0777);
umask($old_umask);
function recurse_copy($src,$dst) {
$dir = opendir($src);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
recurse_copy($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
//echo "$src";
}
$dir = $_POST['name'];
$src = "/home/user/public_html/directory/subdirectory/source/";
$dst = "/home/user/public_html/directory/subdirectory/destination/$dir/";
recurse_copy($src,$dst);
}

Categories