Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    mjs
    mjs is offline
    New Coder
    Join Date
    Jan 2010
    Posts
    97
    Thanks
    17
    Thanked 0 Times in 0 Posts

    auto copy directory and all file inside, help?

    Hi.. All members

    i need your full suggestions and Thanks a lot and sorry for bothering.. I'm still a PHP rookie

    i looking for php code how to auto copy directory and all file inside, to another directory and also auto change the directory/file permission after copied.

    i found the similar from this forum but that not enough to solve my problem. because there no auto change the directory/file permission after copied

    PHP Code:
    <?php
    copydir
    ("admin","filescreate");
    echo 
    "done";

    function 
    copydir($source,$destination)
    {
    if(!
    is_dir($destination)){
    $oldumask umask(0); 
    mkdir($destination01777); // so you get the sticky bit set 
    umask($oldumask);
    }
    $dir_handle = @opendir($source) or die("Unable to open");
    while (
    $file readdir($dir_handle)) 
    {
    if(
    $file!="." && $file!=".." && !is_dir("$source/$file"))
    copy("$source/$file","$destination/$file");
    }
    closedir($dir_handle);
    }
    ?>
    Regards.

  • #2
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    The key term to CHMOD all files in a directory is "recursively". I did a quick Google search and found this:

    http://bjw.co.nz/developer/php/61-ph...ry-recursively

    A further search might turn up something better.



    ----------------------------
    Leonard Whistler

  • #3
    mjs
    mjs is offline
    New Coder
    Join Date
    Jan 2010
    Posts
    97
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Thank so much you give a helpful link, here i posted the code:

    PHP Code:
    <?php
    function chmodDirectory$path .$level ){  
    $ignore = array( ‘cgi-bin’...’ );
    $dh = @opendir$path );
    while( 
    false !== ( $file readdir$dh ) ) ){ // Loop through the directory
    if( !in_array$file$ignore ) ){
    if( 
    is_dir“$path/$file” ) ){
    chmod(”$path/$file”,0777);
    chmodDirectory“$path/$file”, ($level+1));
    } else {
    chmod(”$path/$file”,0777); // desired permission settings
    }//elseif
    }//if in array
    }//while
    closedir$dh );
    }
    //function
    chmodDirectory(”the_directory/,0);
    ?>
    as i said at the my first posted.. i am new rookie to php, by seeing the two code that may possible to joint them but i don't know how to do.
    any help for me would be great thank you.

    how i want it work?
    first, let say i have a directory "data"
    the "data" directory it self already cmod to 755, and i want to copy "data" directory including all file inside, let say become "data1" and then auto cmod the "data1" to 644 after copy finished
    that possible?

    Regards..

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    I did a quick test with the code below and it appears to be OK, I got it from the PHP manual at http://php.net/manual/en/function.copy.php.

    Call this function using the last line of code at bottom.
    PHP Code:
    <?php
    function smartCopy($source$dest$folderPermission=0755,$filePermission=0644){
    # source=file & dest=dir => copy file from source-dir to dest-dir
    # source=file & dest=file / not there yet => copy file from source-dir to dest and overwrite a file there, if present

    # source=dir & dest=dir => copy all content from source to dir
    # source=dir & dest not there yet => copy all content from source to a, yet to be created, dest-dir
        
    $result=false;
       
        if (
    is_file($source)) { # $source is file
            
    if(is_dir($dest)) { # $dest is folder
                
    if ($dest[strlen($dest)-1]!='/'# add '/' if necessary
                    
    $__dest=$dest."/";
                
    $__dest .= basename($source);
                }
            else { 
    # $dest is (new) filename
                
    $__dest=$dest;
                }
            
    $result=copy($source$__dest);
            
    chmod($__dest,$filePermission);
            }
        elseif(
    is_dir($source)) { # $source is dir
            
    if(!is_dir($dest)) { # dest-dir not there yet, create it
                
    @mkdir($dest,$folderPermission);
                
    chmod($dest,$folderPermission);
                }
            if (
    $source[strlen($source)-1]!='/'# add '/' if necessary
                
    $source=$source."/";
            if (
    $dest[strlen($dest)-1]!='/'# add '/' if necessary
                
    $dest=$dest."/";

            
    # find all elements in $source
            
    $result true# in case this dir is empty it would otherwise return false
            
    $dirHandle=opendir($source);
            while(
    $file=readdir($dirHandle)) { # note that $file can also be a folder
                
    if($file!="." && $file!="..") { # filter starting elements and pass the rest to this function again
    #                echo "$source$file ||| $dest$file<br />\n";
                    
    $result=smartCopy($source.$file$dest.$file$folderPermission$filePermission);
                    }
                }
            
    closedir($dirHandle);
            }
        else {
            
    $result=false;
            }
        return 
    $result;
        }

    // call function with this line
    smartCopy("source_folder_name""dest_folder_name"); 

    ?>

    --------------------
    Leonard Whistler

  • Users who have thanked Len Whistler for this post:

    mjs (01-23-2010)

  • #5
    mjs
    mjs is offline
    New Coder
    Join Date
    Jan 2010
    Posts
    97
    Thanks
    17
    Thanked 0 Times in 0 Posts
    well, many thank for your great tips code, it seem work at my desktop with win-XP HE + XAMP, but i don't test it in web server yet, so i don't know if the cmod work or not...

    hm... now would you go on to the next...? adding command button (submit button), mean we need it run (execute) by pushing the submit button.

    Code:
    <form method="post" name="frm" action="copy.php"/> <tr><td><input name="Send" type="submit" value="Submit" /></td></tr> </form>';
    copy.php it self as your current code (smartcopy). that's i am wrong? or do you /someone else have any great idea?
    Last edited by mjs; 01-23-2010 at 04:50 AM.

  • #6
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Both files in the same folder.

    some_page.php
    PHP Code:
    <form method="post" name="frm" action="copy.php"/> <tr><td><input name="Send" type="submit" value="Submit" /></td></tr> </form>; 

    copy.php
    Add this line at the end of the code posted in post #4 it will redirect you back to some_page.php once the php has been executed.
    PHP Code:
    header("Location: some_page.php"); 
    NOTE: for the redirect to work you can't have any white space or html outputted. I think that code in post #4 will be OK.



    ---------
    Last edited by Len Whistler; 01-23-2010 at 07:18 AM.
    Leonard Whistler


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •