PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Jul 04, 2020 1:58 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed May 18, 2016 9:41 am 
Offline
Forum Commoner

Joined: Wed Jul 15, 2009 9:08 pm
Posts: 56
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

Syntax: [ Download ] [ Hide ]
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);
        }


Top
 Profile  
 
PostPosted: Wed May 18, 2016 3:00 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
I don't think sftp supports recursive copying. Use rsync, mget or scp -r.

You could fetch the directory and loop manually, but that would probably be a lot of work.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Tue May 24, 2016 5:05 pm 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 367
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.

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
PostPosted: Tue May 24, 2016 6:23 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
That could be done as a web service. Send a HTTP request for a path and have the target server zip up the directory and return it as the response.

_________________
(#10850)


Top
 Profile  
 
PostPosted: Tue May 24, 2016 10:25 pm 
Offline
Forum Contributor

Joined: Fri Jul 20, 2007 11:29 am
Posts: 367
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.

_________________
Warning: I have no idea what I'm talking about.


Top
 Profile  
 
PostPosted: Wed May 25, 2016 2:33 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US
find /path/to/files -depth -type d

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 33 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group