1

I'm running the following script inside a Laravel job (OperatorSubmissionJob) to upload multiple files to the SFTP server. Everything working well. But I have to disconnect the SFTP connection to end the session after the uploading finished.

$count=1;
$sftp = Storage::disk('sftp');
foreach ($request['image'] as $key => $image) {
    $image_array_1 = explode(";", $image);
    $image_array_2 = explode(",", $image_array_1[1]);
    $data = base64_decode($image_array_2[1]);
    $imageName = Carbon::now()->format('Ymd_his') . $key . '.jpeg';

    // FTP server upload
    $ftp_image_name = $ftp_path . "_" . $count . '.jpeg';
    try {
        $isUploaded = $sftp->put("$ftp_path/" . $ftp_image_name, $data, 'public');
        if ($isUploaded) {
            Log::info("FTP image uploaded (UserID-" . $id . ", CustomerID-" . $mail_history->CustomerID . "): " . $count);
        } else {
            Log::info("FTP image upload failed (UserID-" . $id . ", CustomerID-" . $mail_history->CustomerID . "): " . $count);
        }
        $count++;
    } catch (\Exception $e) {
        Log::error($e->getMessage());
    }
}
// have to disconnect $sftp here. I have tried the following:
// $sftp->disconnect(); // error: undefined method disconnect
// $sftp->getDriver()->getAdapter()->disconnect(); // error: undefined method disconnect
// $sftp->getFilesystem()->getAdapter()->getClient()->disconnect();
// 

Used techs: Laravel 9 league/flysystem-sftp-v3

None of the disconnect scripts working in my case. Please help..

I have tried with the following scripts:

$sftp->disconnect();
$sftp->getDriver()->getAdapter()->disconnect();
$sftp->getFilesystem()->getAdapter()->getClient()->disconnect();
1
  • Have you tried disconnect it using Storage::disk('sftp')->getAdapter()->disconnect(); / $sftp->getAdapter()->disconnect();? Commented Apr 25, 2023 at 8:38

1 Answer 1

0

You could try to unset the variable holding the reference to your disk.

unset($sftp);

See: https://laracasts.com/discuss/channels/laravel/how-to-disconnect-from-storagedisk

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.