Creating a domain to domain FTP script?

Creating a domain to domain FTP script?

I am trying to create a script that will transfer all the files contained in an inputted local directory of a domain; then copy those files on a remote domain.

All folder and filenames must be duplicated on the remote domain.

I want to use PHP’s FTP functions to copy files to the remote server.

Here is my question. Let’ say for example the script generated the following array of absolute paths to files on the local domain:


$list_of_files[] = '/home/username/public_html/ftptest/doc1.txt';
$list_of_files[] = '/home/username/public_html/ftptest/doc2.txt';
$list_of_files[] = '/home/username/public_html/ftptest/sub/doc1.txt';

If I was attempting to use ftp_put() to copy files to the target domain; then could I use ftp_put() directly to replicate the directory structure and paths on the target domain.

Or would I first have recreate each folder and directory on the target domain then have the PHP script change to the target directory before using ftp_put() to copy the files?

You could recreate the directory stucture and then go and copy all the files.
Or you may want to look into zip archive files in php.
Perhaps you could just zip what you want copied, then transfer that and then collapse it once it gets there. This way is often faster on the transfer, as it doesnt have to do everything file by file.

it’s… ugly…and i recommend using zip-transfer as smftre siad (or… just a regular FTP client…) but…

(PS, i’m assuming your $list_of_files has disposed of . and … automatically, and $ftp is your FTP-Resource object)


$dirs = array();
foreach($list_of_files AS $file) {
  $path = explode('/',$file);
  $string = "";
  for($i = 0; $i < (count($path) - 1); $i++) {
     $string .= '/'.$path[$i];
     if(!array_search($string,$dirs) && !is_dir($string)) {
        mkdir($string);
        $dirs[] = $string;
     }
  }
  ftp_get($ftp,$file,$file,FTP_BINARY);
}

PS: This assumes that the absolute path-to your website are the same - very rarely is that the case if you’re not hosting the files yourself…

Thank you for responding.

Just to be clear, when copying files from domain to another domain, the directory structure MUST be created first , before the files are copied to their respective absolute directory paths?


ftp_put ( resource ftp_stream, string remote_file, string local_file, int mode [, int startpos] )

Ftp_put() will not do it automatically if I use “/home/username/public_html/ftptest/sub/doc1.txt” as the “string remote_file”?

Other words, I would have to first make the “ftptest” and “sub” directories? Then have the script change to the “/ftptest/sub/” directory? Finally, copy the file “doct1.txt” to the “/ftptest/sub/” directory?

The zip file is an excellent suggestion. I have never used PHP to create zip files before; but it should not be to hard to figure out.

However, my only concern with using zip file is this. In the pass, when an zip file was uploaded to a domain; I had to manually unzip the file using cpanel’s file manager.

I would like to avoid that manual step and have the script do every thing automatically.

Is it possible to have a PHP script remotely unzip a file on a different domain?

I would use zip_open to read the contents of the zip and extract the data to the local filesystem.

My webhost does include Zip functions in my current hosting plan. I would have to upgrade to VPS. I am unwilling to do this.

There fore I will create a script to manually recreate the directory structure ftp files on the remote domains.

The script above should create the structure as it goes.