ssh2_scp_send() using php corrupts pdf

强颜欢笑 提交于 2019-12-11 01:37:59

问题


I am having a problem when I am sending a pdf file to my server.

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

when I send pdf. it creates a pdf file in target location but its corrupted.


回答1:


Try SFTP. Examples follow.

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

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


来源:https://stackoverflow.com/questions/19944374/ssh2-scp-send-using-php-corrupts-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!