Skyfe
Skyfe

Reputation: 581

PHP FTP copy directory recursive

For my website I need to copy one folder from the main account on my VPS to a (automaticly) newly created cPanel account. I tried to do this with PHP, through FTP using the following code (function):

function ftp_sync ($dir) { 
    global $conn_id;

    if ($dir != ".") { 
            if (ftp_chdir($conn_id, $dir) == false) { 
                echo ("Change Dir Failed: $dir<BR>\r\n"); 
                return; 
            } 
            if (!(is_dir($dir))) 
                mkdir($dir); 
        chdir ($dir); 
    } 

        $contents = ftp_nlist($conn_id, "."); 
    foreach ($contents as $file) { 

            if ($file == '.' || $file == '..') 
            continue; 

            if (@ftp_chdir($conn_id, $file)) { 
                    ftp_chdir ($conn_id, ".."); 
                    ftp_sync ($file); 
        } 
            else 
            ftp_get($conn_id, $file, $file, FTP_BINARY); 
        }
    }

    foreach (glob("*") as $file)
    {
        if(substr_count($file, ".") > 0)
        {
            $source_file = $file;
            $destination_file = $file;
            $upload = ftp_put($conn_id, "public_html/".$destination_file, $source_file, FTP_BINARY);
            echo "<br />";
            // check upload status
            if (!$upload) { 
                echo "FTP upload has failed!";
            } else {
                echo "Uploaded $source_file to $ftp_server as $destination_file";
            }
        }else{
            ftp_sync($dir);
        }
    } 


    ftp_chdir ($conn_id, "..");
        chdir ("..");  

} 

However it doesn't seem to work (no new directories are created and uploaded to)... Does anyone know why this isn't working and how I can make it work?

Thanks in advance!

Best Regards, Skyfe.

EDIT: I forgot to mention that I run the script as a cronjob script, also making sure it has all rights as it's executed from the main server.

Upvotes: 0

Views: 3587

Answers (2)

Skyfe
Skyfe

Reputation: 581

Didn't get the function to work so I recreated it my own way and got it working!

...
ftp_mkdir($conn_id, "public_html/".$dir);
ftp_upload($dir);

// close the FTP stream 
ftp_close($conn_id); 

function ftp_upload($dir) {
    global $conn_id;
    if($handle = opendir($dir))
    {
    while(false !== ($file = readdir($handle)))
    {
        if($file != "." && $file != ".." && $file != "...") {
        if(substr_count($file, ".") > 0)
        {
            $full_dir = "public_html/".$dir;
            $source_file = $file;
            $destination_file = $file;
            $upload = ftp_put($conn_id, $full_dir."/".$destination_file, $dir."/".$source_file, FTP_BINARY);
            echo "<br />";
            // check upload status
            if (!$upload) { 
                echo "FTP upload has failed!"; 
            } else {
                echo "Uploaded ".$source_file." to ".$ftp_server." as ".$destination_file; 
            }
        }else{
            ftp_mkdir($conn_id, "public_html/".$dir."/".$file);
            ftp_upload($dir."/".$file);
        }
        }
    }   
    }

}

Now the only thing left is making sure it works on big directory structures too (without a huge loadtime)!

Upvotes: 0

superice
superice

Reputation: 11

First of all, make sure your directory on the target server is writable. Temporary chmodding it to 0777 should help. The rest of your script seems to be okay. You could try setting the error logging to all errors (just add error_reporting(E_ALL); at the start of your script). PHP should then output every error, warning or notice, which might provide you more info.

Upvotes: 1

Related Questions