2

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.

Malav Shah
  • 143
  • 2
  • 16

1 Answers1

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));
neubert
  • 15,947
  • 24
  • 120
  • 212