Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jijoel's avatar
Level 10

Using scp to download files from remote server?

Aloha,

Could someone please help me with some things that I could look at in order to download files from a remote server?

I am attempting to use scp to download the files from the remote server. The code in my controller is just this:

public function getTest()
{
    exec("scp [email protected]:/path/to/files/* /local/path/");
}

If I run the exec command with php -r, it works. Similarly, if I open tinker, and run (new App\Http\Controllers\TestController)->getTest(), it works.

I have found that it does not work when I open the page in a browser. It also does not work if I run it as a scheduled job. It does work if I manually run the exec command from artisan, though.

Right now, I'm doing this on a homestead box. 'vagrant' is both the local user and the web user, so I believe that access should be the same? Is there any reason it wouldn't be?

I have also tried using the -i option to explicitly set the key file. I've also tried setting up a ~/.ssh/config file.

I've spent all day on this, and am now thoroughly confused. Any ideas on things to look at?

Thank you,

--joel

0 likes
4 replies
jijoel's avatar
Level 10

ssh2_scp_recv worked. Thank you so much!

My code ended up looking like this:

private function makeFileConnection()
{
    $connection = ssh2_connect(
        config('web.file-server'),
        config('web.port')
    );
    if( ! $connection ) {
        error('unable to establish file connection');
        return;
    }

    $fingerprint = config('web.fingerprint');
    if ($fingerprint) {
        if ($fingerprint !== ssh2_fingerprint(
            $connection,
            SSH2_FINGERPRINT_MD5 | SSH2_FINGERPRINT_HEX
        )) {
            error("HOSTKEY MISMATCH!\n" .
                "Possible Man-In-The-Middle Attack?");
            return;
        }
    }

    if (! ssh2_auth_pubkey_file (
        $connection,
        config('web.user'),
        config('web.public-key'),
        config('web.private-key')
    )) {
        error('unable to authenticate with server');
        return;
    }

    $this->connection = $connection;
}

... and, for each user:

    $file_dir = storage_path("app/files/$user_dir/");
    if (! File::isDirectory($file_dir))
        File::makeDirectory($file_dir, 0755, true);

    $attachments = json_decode($application->attachments);
    foreach($attachments as $attachment) {
        if (! ssh2_scp_recv(
            $this->connection,
            config('web.source').$attachment,
            $file_dir . File::basename($attachment)
        )) {
            error('Unable to copy '.$attachment);
        } else {
            // delete the file
            if (! config('web.test-mode',true)) {
                $sftp = ssh2_sftp($this->connection);
                ssh2_sftp_unlink($sftp, config('web.source').$attachment);
            }
        }
    }
normkatz's avatar

Did you have to install ssh2 PECL extension or was it already in your server?

jijoel's avatar
Level 10

I had to install it. Easy enough, though:

sudo apt-get install php-ssh2

Please or to participate in this conversation.