简体   繁体   English

使用PHP的ssh2_scp_send()会损坏pdf

[英]ssh2_scp_send() using php corrupts pdf

I am having a problem when I am sending a pdf file to my server. 将pdf文件发送到服务器时出现问题。

my script works when I try to send .csv file but the problem occurs when I try to send a pdf file 当我尝试发送.csv文件时,我的脚本有效,但是当我尝试发送pdf文件时,出现了问题

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

when I send pdf. 当我发送pdf时。 it creates a pdf file in target location but its corrupted. 它在目标位置创建了一个pdf文件,但已损坏。

Try SFTP. 尝试使用SFTP。 Examples follow. 示例如下。

With libssh2: 使用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));

Although personally I'd recommend you use phpseclib, a pure PHP SFTP implementation instead. 尽管我个人建议您使用phpseclib,它是纯PHP SFTP实现 It has a number of advantages over libssh2. 与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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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