简体   繁体   中英

How can I write a file on an sftp-server with PHP?

I hope someone here could help me, because I couldn't find any solution with Google. What I have to do is to generate a XML-string (that works) an save that directly into a file on an sftp-server.

So far, so good... I used the following code with ftp and it works to, but not with ftps. So I either need another options-configuration for the stream or a different way to solve that task.

Here 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);

You need a PHP with OpenSSL support compiled in. Then, use ftps:// instead of ftp://. More info on FTPS .

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

一种更简单的解决方案是将文件保存在临时目录中,然后执行shell命令以实际发送文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM