ssh2_scp_send() 使用 php 破坏 pdf [英] ssh2_scp_send() using php corrupts pdf
问题描述
我在向服务器发送 pdf 文件时遇到问题.
I am having a problem when I am sending a pdf file to my server.
当我尝试发送 .csv 文件时我的脚本有效,但当我尝试发送 pdf 文件时出现问题
my script works when I try to send .csv file but the problem occurs when I try to send a pdf file
<?php
$user= "username";
$pass= "password";
$src= "/home/desktop/myfile.pdf";
$trg= "/server/path/myfile.pdf";
$con = ssh2_connect('myserver.com', 22);
ssh2_auth_password($con, $user, $pass);
ssh2_scp_send($con, $src, $trg);
?>
当我发送pdf时.它在目标位置创建了一个 pdf 文件,但已损坏.
when I send pdf. it creates a pdf file in target location but its corrupted.
推荐答案
尝试 SFTP.示例如下.
Try SFTP. Examples follow.
使用 libssh2:
With libssh2:
<?php
$ssh = ssh2_connect('www.domain.tld');
ssh2_auth_password($ssh, 'username', 'password');
$sftp = ssh2_sftp($ssh);
$fp = fopen('ssh2.sftp://'.$sftp.'/home/username/1mb', 'w');
fwrite($fp, str_repeat('a', 1024 * 1024));
尽管我个人建议您使用 phpseclib,一个纯 PHP SFTP 实现.与 libssh2 相比,它有许多优点.IE.它速度更快,并且具有更好的公钥支持:
Although personally I'd recommend you use phpseclib, a pure PHP SFTP implementation instead. It has a number of advantages over libssh2. ie. it's faster and has better public key support among other things:
<?php
include('Net/SFTP.php');
$sftp = new Net_SFTP('www.domain.tld');
$sftp->login('username', 'password');
$sftp->put('1mb', str_repeat('a', 1024 * 1024));
这篇关于ssh2_scp_send() 使用 php 破坏 pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!