PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
This function only copies files in a folder from remote to source.But I need to modify this function to copy all the folders and its contents..Eg: FolderA contains Folder1,Folder2,Folder3, and Folder 1 contains file1,file2 and file3.Please help
public function CopyFile($remote_file, $local_file)
{
$sftp = $this->sftp;
$stream = @fopen("ssh2.sftp://$sftp$remote_file", 'r');
if (! $stream)
throw new \Exception("Could not open file: $remote_file");
//$contents = stream_get_contents($stream);
$contents=@file_get_contents($remote_file);
@fclose($stream);
$stream = @fopen("ssh2.sftp://$sftp$local_file", 'w');
if (! $stream)
throw new \Exception("Could not open file: $local_file");
if (@fwrite($stream, $contents) === false)
throw new \Exception("Could not send data from file: $local_file.");
//file_put_contents ($local_file, $contents);
@fclose($stream);
$this->deleteFile($remote_file);
}
If you control the destination server as well you could maybe zip up the entire folder structure, ftp the zip, and then extract the entire folder structure on the destination server.
Also I think there is some bash terminal command that will print out the path of every folder & file starting with the deepest nesting and moving up to the present working directory root. You could turn the output from that into an array and loop over it to guide the ftp uploads.