Copying files from local directory to remote server

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 files from local directory to remote server

Post by sobha »

How do I copy the pdf and csv files in the local folder to a folder in remote server using symfony?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Copying files from local directory to remote server

Post by requinix »

You can't copy files across servers. You'll have to use something like FTP or an SSH tunnel, and what you do depends on what the remote server supports...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Copying files from local directory to remote server

Post by Christopher »

sobha wrote:using symfony?
Yes, you would use some program on your system to do that. Are the source and target servers Linux or Windows?
(#10850)
sobha
Forum Commoner
Posts: 56
Joined: Wed Jul 15, 2009 9:08 pm

Re: Copying files from local directory to remote server

Post by sobha »

Code: Select all

public function uploadFile($local_file, $remote_file)
	{
		$sftp = $this->sftp;
		$stream = @fopen("ssh2.sftp://$sftp$remote_file", 'w');
		if (! $stream)
			throw new \Exception("Could not open file: $remote_file");
		$data_to_send = @file_get_contents($local_file);
		//$contents = stream_get_contents($stream);
		if ($data_to_send === false)
			throw new \Exception("Could not open local file: $local_file.");
		if (@fwrite($stream, $data_to_send) === false)
			throw new \Exception("Could not send data from file: $local_file.");
		@fclose($stream);
		return $data_to_send;
	}
This is my upload file function to upload pdf and csv. Only the files are uploaded..But not the content.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Copying files from local directory to remote server

Post by Christopher »

I assume that $data_to_send actually contains data? And that fwrite() is returning the number of bytes written? Maybe don't suppress errors to see what is going wrong?
(#10850)
Post Reply