How to write a file on sftp server with PHP?

I hope someone here can help me because I could not find a solution with Google. I need to create an XML string (which works) that will be saved directly to a file on the sftp server.

So far so good ... I have used the following code with ftp and it works, but not with ftps. Therefore, I either need another parameter setting for the stream, or another way to solve this problem.

Here is my current code:

$host = 'ftp.example.com'; $port = 22; $user = 'xxxxxx'; $pass = 'xxxxxx'; $file = 'test_' . time() . '.txt'; $ftpPath = sprintf('ftp://%s:% s@ %s:%d/%s', $user, $pass, $host, $port, $file); $context = stream_context_create(array('ftp' => array('overwrite' => true))); file_put_contents($ftpPath, 'test', 0, $context); 
+4
source share
3 answers

You need PHP with OpenSSL support. Then use ftps: // instead of ftp: //. Learn more about FTPS .

+1
source

A simpler solution would be to save the file in a temporary directory and then execute a shell command to actually send the file.

0
source

Use ssh2.sftp:// instead of ftp:// . See ssh2_sftp .

0
source

Source: https://habr.com/ru/post/1313164/


All Articles