Copying contents of a folder

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!

Moderator: General Moderators

Post Reply
sobha
Forum Commoner
Posts: 56
Joined: Wed Jul 15, 2009 9:08 pm

Copying contents of a folder

Post by sobha »

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

Code: Select all

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);
	}
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Copying contents of a folder

Post by Christopher »

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)
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Copying contents of a folder

Post by thinsoldier »

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.
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Copying contents of a folder

Post by Christopher »

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)
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Copying contents of a folder

Post by thinsoldier »

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.
User avatar
Christopher
Site Administrator
Posts: 13592
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Copying contents of a folder

Post by Christopher »

find /path/to/files -depth -type d
(#10850)
Post Reply