Jump to content

Directory Copy


andyd34

Recommended Posts

I have the following script, sometimes it works great and sometimes it only copies a few files which isnt much use

static public function recurse_copy($src, $dst) {

$dir = opendir($src);
  $result = ($dir === false ? false : true);
    if ($result !== false) {
      if (!is_dir($dst)) {
        $result = @mkdir($dst);
      }
      if ($result === true) {
        while(false !== ( $file = readdir($dir)) ) { 
	  if (( $file != '.' ) && ( $file != '..' ) && $result) { 
	    if ( is_dir($src . '/' . $file) ) { 
	      $result = self::recurse_copy($src . '/' . $file,$dst . '/' . $file); 
              set_time_limit(5);
	    } else { 
	      $result = copy($src . '/' . $file,$dst . '/' . $file); 
              set_time_limit(5);
	    } 
          set_time_limit(5);
	} 
      } 
    closedir($dir);
  }
}
return $result;
}

Does anyone have a better more stable solutions. The folder is 14mb

 

Any help would be nuch appreciated

Link to comment
Share on other sites

You state

 

 

sometimes it works great and sometimes it only copies a few files

 

Do you get those results on the same source and destination directories?

 

Aside from your specific issue, I see some things that could be improved.

 

 

$result = ($dir === false ? false : true);
if ($result !== false) {

 

The condition for setting $result will return a Boolean (true/false), so there is no need to create a ternary operator to assigne true/false. Plus, since $result will only be true/false, there is no need for the explicit type check on the if() condition. Additionally, the reuse of $result for many things throughout the logic could lead to confusion. But, more importantly, there is no error handling within the code when errors do occur. So, of course you don't know what the problem may be.

 

One problem in your code that may be the source of the copy problem is the check in the if() condition for $result for the while loop condition. 

 

if (( $file != '.' ) && ( $file != '..' ) && $result)

 

Because in the loop there is this:

 

$result = copy($src . '/' . $file,$dst . '/' . $file);

 

If that copy fails for any file it will skip all other files in the source directory because $result will never get reset (i.e. will be false for the rest of the execution). So, my guess is that there is a file that can't be copied for some reason (locked?) that is causing your problems. This is exactly what I was referring to above about reusing the same variable for different things.

 

Here is a quick rewrite that fixes some problems and adds some error handling. This is not ready for production use (should never display system errors to the user), but it should give you an idea of what the problem may be

 

static public function recurse_copy($src, $dst)
{
    $dir = opendir($src);
    //Open the source directory
    if (!$dir)
    {
        echo "Unable to open source directory '{$src}'";
        return false;
    }
    //Create the destination directory
    if (!is_dir($dst))
    {
        if(!@mkdir($dst))
        {
            echo "Unable to create destingation directory '{$dst}'";
            return false;
        }
    }
    //Copy the files
    while(false !== ( $file = readdir($dir)) )
    {
        if (( $file == '.' ) || ( $file == '..' ))
        {
            //Skip . and ..
            continue;
        }
        //Create variables instead of concatenating same strings multiple times
        $srcPath = $src.'/'.$file;
        $dstPath = $dst.'/'.$file;
        if ( is_dir($srcPath) )
        {
            $result = self::recurse_copy($srcPath, $dstPath);
        }
        else
        {
            if(!copy($srcPath, $dstPath))
            {
                echo "Error copying file '{$srcPath}'<br>\n";
            }
        }
        set_time_limit(5);
    } 
    closedir($dir);
    return true;
}
Link to comment
Share on other sites

Thanks for the replies, i think i've found the main issue.

 

its all part of an automation process

 

  1. Subdomain Creation
  2. Database User Creation
  3. Database Creation
  4. User added to database
  5. Database tables added
  6. Files moved to subdomain

when i've split everything up i'm getting a server 500 EMPTY_RESPONSE error on steps 2, 3 and 4.

$this->xmlapi->set_port( 2082 );
$this->xmlapi->password_auth($cPanelUser,$cPanelPass);
$this->xmlapi->set_debug(1); //output actions in the error log 1 for true and 0 false 
$this->xmlapi->api1_query($cPanelUser, 'SubDomain', 'addsubdomain', array($subDomain,$rootDomain,0,0, $dir));
set_time_limit(10);
$this->xmlapi->api1_query($cPanelUser, "Mysql", "adduser", array($databaseuser, $databasepass));
set_time_limit(10);
$this->xmlapi->api1_query($cPanelUser, "Mysql", "adddb", array($databasename)); 
set_time_limit(10);
$addusr = $this->xmlapi->api1_query($cPanelUser, "Mysql", "adduserdb", array($databasename, $databaseuser, 'all'));

I've tried extending the time out but it didnt work. I'll just have to keep plugging away at it

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.