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 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    156
    Thanks
    8
    Thanked 1 Time in 1 Post

    directory copy with chown

    i'm using the script below to make a directory, then copy the contents of another directory into the new one. Problem is... its not setting the owner, so I can't delete/modify the files. Can someone point out how I can chown the files as they are copied?

    Thanks in advance.

    PHP Code:
    <?php
    $firstName
    =$_REQUEST['firstName'];
    $lastName=$_REQUEST['lastName'];

    mkdir('../../'.$firstName.$lastName0777);

    function 
    copydirr($fromDir,$toDir,$chmod=0757,$verbose=false)
    {
    //* Check for some errors
    $errors=array();
    $messages=array();
    if (!
    is_writable($toDir))
        
    $errors[]='target '.$toDir.' is not writable';
    if (!
    is_dir($toDir))
        
    $errors[]='target '.$toDir.' is not a directory';
    if (!
    is_dir($fromDir))
        
    $errors[]='source '.$fromDir.' is not a directory';
    if (!empty(
    $errors))
        {
        if (
    $verbose)
            foreach(
    $errors as $err)
                echo 
    '<strong>Error</strong>: '.$err.'<br />';
        return 
    false;
        }
    //*/
    $exceptions=array('.','..');
    //* Processing
    $handle=opendir($fromDir);
    while (
    false!==($item=readdir($handle)))
        if (!
    in_array($item,$exceptions))
            {
            
    //* cleanup for trailing slashes in directories destinations
            
    $from=str_replace('//','/',$fromDir.'/'.$item);
            
    $to=str_replace('//','/',$toDir.'/'.$item);
            
    //*/
            
    if (is_file($from))
                {
                if (@
    copy($from,$to))
                    {
                    
    chmod($to,$chmod);
                    
    touch($to,filemtime($from)); // to track last modified time
                    
    $messages[]='File copied from '.$from.' to '.$to;
                    }
                else
                    
    $errors[]='cannot copy file from '.$from.' to '.$to;
                }
            if (
    is_dir($from))
                {
                if (@
    mkdir($to))
                    {
                    
    chmod($to,$chmod);
                    
    $messages[]='';
                    }
                else
                    
    $errors[]='cannot create directory '.$to;
                
    copydirr($from,$to,$chmod,$verbose);
                }
            }
    closedir($handle);
    //*/
    //* Output
    if ($verbose)
        {
        foreach(
    $errors as $err)
            echo 
    '<strong>Error</strong>: '.$err.'<br />';
        foreach(
    $messages as $msg)
            echo 
    $msg.'<br />';
        }
    //*/
    return true;
    }

    copydirr('memberTemplate','../../'.$firstName.$lastName,0777,true);
    ?>

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Brisbane, Australia
    Posts
    181
    Thanks
    1
    Thanked 0 Times in 0 Posts
    http://au3.php.net/chmod

    of course you'd want to chmod to 0777
    -mR_eGo
    _______________________
    Programming since
    3 years old.

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    156
    Thanks
    8
    Thanked 1 Time in 1 Post
    Quote Originally Posted by mr_ego View Post
    http://au3.php.net/chmod

    of course you'd want to chmod to 0777
    Thanks, but as you can see from the above script, they are being CHMOD'd to 777. If you look at what I posted, they are being 'owned' by root and not by me (shared hosting). Please read entirely before posting as most people only read them if there are 0 replies.


  •  

    Posting Permissions

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