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

tarikulwebx's avatar

SFTP adapter disconnect() method doesn't exists

Hi, 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();
// 

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

$sftp->disconnect() showing Call to undefined method League\Flysystem\Filesystem::disconnect(). $sftp->getDriver()->getAdapter()->disconnect() showing Call to undefined method League\Flysystem\PhpseclibV3\SftpAdapter::disconnect() and $sftp->getFilesystem()->getAdapter()->getClient()->disconnect(); showing Call to undefined method League\Flysystem\Filesystem::getFilesystem()

0 likes
4 replies
Sinnbeck's avatar

Can you describe it more than "not working"? Error?

Flex Max's avatar

@Sinnbeck Hello. I had the same error. It happened when I updated laravel version v8 to v9 and changed library "league/flysystem-sftp": "^1.0", on "league/flysystem-sftp-v3": "^3.6.0",

Storage::disk($storageName)->getAdapter()->disconnect();

Call to undefined method League\Flysystem\PhpseclibV3\SftpAdapter::disconnect() {"exception":"[object] (Error(code: 0): Call to undefined method League\Flysystem\PhpseclibV3\SftpAdapter::disconnect()

I still don't know how to resolve it.

feherlofia's avatar

@Flex Max i was having the same issue and its apparently because you cant manually disconnect a sftp connection manually anymore. using phpseclib or something similar is recommended

Please or to participate in this conversation.