andyd34 Posted June 12, 2016 Share Posted June 12, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/301331-directory-copy/ Share on other sites More sharing options...
ginerjm Posted June 12, 2016 Share Posted June 12, 2016 Do you have php error checking turned on for any messages, or have you checked the log? Is this a timing issue? I see you play with the time limit - do you know if the problem occurs during a longer execution time? Quote Link to comment https://forums.phpfreaks.com/topic/301331-directory-copy/#findComment-1533599 Share on other sites More sharing options...
Psycho Posted June 13, 2016 Share Posted June 13, 2016 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; } Quote Link to comment https://forums.phpfreaks.com/topic/301331-directory-copy/#findComment-1533622 Share on other sites More sharing options...
andyd34 Posted June 14, 2016 Author Share Posted June 14, 2016 Thanks for the replies, i think i've found the main issue. its all part of an automation process Subdomain Creation Database User Creation Database Creation User added to database Database tables added 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 Quote Link to comment https://forums.phpfreaks.com/topic/301331-directory-copy/#findComment-1533641 Share on other sites More sharing options...
andyd34 Posted June 14, 2016 Author Share Posted June 14, 2016 Just a quick note, Ive just tested in firefox and everything is working fine. When testing in Chrome and IE its throwing the error Quote Link to comment https://forums.phpfreaks.com/topic/301331-directory-copy/#findComment-1533645 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.