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

ctyler's avatar

SFTP download failure after several minutes

Good morning. I am working on a php artisan command to check a remote file via SFTP and download it. However it fails after 1 hour and 7 minutes on my local machine (windows running wamp) and it fails on my remote server after 17 minutes (ununtu 22.04, PHP 8.1, Laravel 10).

<?php

use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;

//Setting to more than is needed
ini_set('memory_limit','123999M');
ini_set('max_execution_time', 0);
ini_set('post_max_size', '10000M');
ini_set('upload_max_filesize', '10000M');

$current = Carbon::now();

$remoteFile = '/home/sql2/transfer/DATAFILE.2014.BAK';

$startTime = now();

if(Storage::disk('sftp')->exists($remoteFile)) {
    //Make sure remote file is not being loaded
    $firstFileSize = Storage::disk('sftp')->size($remoteFile);
    sleep(2);
    $secondFileSize = Storage::disk('sftp')->size($remoteFile);
    if($firstFileSize == $secondFileSize) {

        $this->info('First filesize check, ' . $firstFileSize . ', is the same as the second, ' . $secondFileSize);
        $this->info('Downloading file - ' . number_format($firstFileSize / 1048576, 2) . ' MB');
        try{
            //Storage::disk('wayne') is local storage at 'app/downloads/wayne'
            Storage::disk('wayne')->putFileAs($remoteFile,Storage::disk('sftp')->get($remoteFile));
        } catch (\Throwable $th) {
            $totalDuration = $startTime->diffInSeconds(now());
            $this->info('Error: ' . $th->getMessage());
            $this->info('Total run time: ' . gmdate('H:i:s', $totalDuration));
            dd($th);
        }
        $totalDuration = $startTime->diffInSeconds(now());
        $this->info('Down load complete!');
        $this->info('Total run time: ' . gmdate('H:i:s', $totalDuration));
    }
}

After a while it fails with this output:

Downloading file - 5,540.90 MB
Error: fopen(/home/sql2/transfer/DATAFILE.2014.BAK): Failed to open stream: No such file or directory
Total run time: 00:17:04
ErrorException {#2033 // app/Console/Commands/ProcessWayneSftp.php:66
  #message: "fopen(/home/sql2/transfer/DATAFILE.2014.BAK): Failed to open stream: No such file or directory"
  #code: 0
  #file: "./vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php"
  #line: 419
  #severity: E_WARNING
  trace: {
    ./vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php:419 { …}
    ./vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:254 { …}
    Illuminate\Foundation\Bootstrap\HandleExceptions->Illuminate\Foundation\Bootstrap\{closure}() {}
    ./vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemAdapter.php:419 { …}
    ./app/Console/Commands/ProcessWayneSftp.php:60 {
      App\Console\Commands\ProcessWayneSftp->handle()^
      › try{
      ›     Storage::disk('wayne')->putFileAs($remoteFile,Storage::disk('sftp')->get($remoteFile));
      › } catch (\Throwable $th) {
    }
    ./vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36 { …}
    ./vendor/laravel/framework/src/Illuminate/Container/Util.php:41 { …}
    ./vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:93 { …}
    ./vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:35 { …}
    ./vendor/laravel/framework/src/Illuminate/Container/Container.php:662 { …}
    ./vendor/laravel/framework/src/Illuminate/Console/Command.php:208 { …}
    ./vendor/symfony/console/Command/Command.php:326 { …}
    ./vendor/laravel/framework/src/Illuminate/Console/Command.php:177 { …}
    ./vendor/symfony/console/Application.php:1081 { …}
    ./vendor/symfony/console/Application.php:320 { …}
    ./vendor/symfony/console/Application.php:174 { …}
    ./vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:201 { …}
    ./artisan:35 { …}
  }
}

Am I doing something wrong? I am not sure how to trouble shoot this. Any suggestions would be appreciated.

0 likes
0 replies

Please or to participate in this conversation.