Recently, I had an error occur where I had to connect to the same SFTP server twice within an hour and access separate directories. The first connection would always work, but the second failed with a "ErrorException: Connection Closed By Server".
After TREMENDOUS amounts of searching, I stumbled over Gabor Javorsky's article "Laravel 5.6, SFTP, connection hygiene and you!"
The basic gist is that the disconnect isn't being called on the SFTP server for me automatically. My fix was writing a one line helper function to disconnect the file system after the file has been obtained.
~~~
/** * $this->remoteDisk is a variable set at the top of the file, which contains * the reference to the disk's configuration name. * * The logException function is a helper that I made that formats the exception and stores * in my database. * * I am not here for your public vs private vs protected discussions. **/ public function closeConnection() { try { Storage::disk($this->remoteDisk)->getAdapter()->disconnect(); return true; } catch (\Exception $ex) { logException((new \ReflectionClass($this))->getShortName(), 'Generic Exception', 0, (new \ReflectionClass($this))->getShortName(), $ex->getMessage()); return false; } }
~~~