Page 1 of 1

Copying files from local directory to remote server

Posted: Fri Apr 08, 2016 5:15 am
by sobha
How do I copy the pdf and csv files in the local folder to a folder in remote server using symfony?

Re: Copying files from local directory to remote server

Posted: Fri Apr 08, 2016 6:21 am
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...

Re: Copying files from local directory to remote server

Posted: Fri Apr 08, 2016 3:35 pm
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?

Re: Copying files from local directory to remote server

Posted: Tue Apr 12, 2016 10:05 am
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.

Re: Copying files from local directory to remote server

Posted: Tue Apr 12, 2016 4:40 pm
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?