Skip to content

Instantly share code, notes, and snippets.

@DominikStyp
Last active August 17, 2023 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DominikStyp/cfbab1a6ef58475864d9d0039bfe635c to your computer and use it in GitHub Desktop.
Save DominikStyp/cfbab1a6ef58475864d9d0039bfe635c to your computer and use it in GitHub Desktop.
Laravel 10: How to replace the FlySystem FtpAdapter and supress/catch FTP Warnings/Errors
<?php
namespace App\Providers;
use App\Contracts\BrandNameResolverInterface as BrandNameResolverServiceContract;
use App\Http\Controllers\Auth\PasswordBrokerManager;
use App\Repositories\Interfaces\AccountInterface;
use App\Services\Filesystem\FtpAdapter;
use App\Services\User\AccountSessionService;
use App\View\Components\Modal;
use Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider;
use Carbon\Carbon;
use Illuminate\Foundation\Application;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
/**
* Class AppServiceProvider
*/
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Carbon::setLocale(config('app.locale'));
}
/**
* Register any application services.
*/
public function register(): void
{
$this->overrideFtpAdapter();
}
/**
* @return void
*/
private function overrideFtpAdapter(): void
{
$this->app->extend(\League\Flysystem\FilesystemAdapter::class, function ($service, $app) {
return app(FtpAdapter::class);
});
$this->app->singleton('filesystem', function ($app) {
return new \App\Services\Filesystem\FilesystemManager($app);
});
}
}
<?php
namespace App\Services\Filesystem;
use Illuminate\Filesystem\FilesystemAdapter;
use League\Flysystem\Ftp\FtpConnectionOptions;
class FilesystemManager extends \Illuminate\Filesystem\FilesystemManager
{
/**
*
* This is only overridden due to the SSL_read error that occurs when using the FTP adapter.
*
* WARNING ftp_fput(): SSL_read on shutdown: error:0A000126:SSL routines::unexpected eof while reading in vendor/league/flysystem-ftp/FtpAdapter.php on line 164.
*
*
* @param array $config
* @return FilesystemAdapter
*/
public function createFtpDriver(array $config)
{
if (! isset($config['root'])) {
$config['root'] = '';
}
$adapter = new FtpAdapter(FtpConnectionOptions::fromArray($config));
return new FilesystemAdapter($this->createFlysystem($adapter, $config), $adapter, $config);
}
}
<?php
namespace App\Services\Filesystem;
use Illuminate\Support\Facades\Log;
use League\Flysystem\Config;
use League\Flysystem\FilesystemAdapter;
use League\Flysystem\FilesystemException;
use League\Flysystem\Ftp\FtpAdapter as FlysystemFtpAdapter;
use Throwable;
use function str_contains;
class FtpAdapter extends FlysystemFtpAdapter implements FilesystemAdapter
{
/**
* This is only overridden due to the SSL_read error that occurs when using the FTP adapter.
*
* WARNING ftp_fput(): SSL_read on shutdown: error:0A000126:SSL routines::unexpected eof while reading in vendor/league/flysystem-ftp/FtpAdapter.php on line 164
*
* @throws FilesystemException
* @throws Throwable
*/
public function writeStream(string $path, $contents, Config $config): void
{
// Force warnings to throw exceptions
set_error_handler(function ($level, $message, $file = '', $line = 0) {
if (error_reporting() & $level) {
throw new \ErrorException($message, 0, $level, $file, $line);
}
});
try {
// dump("writing stream...");
parent::writeStream($path, $contents, $config);
} catch (Throwable $exception) {
// if we see "ftp_fput(): SSL_read on shutdown: error:0A000126:SSL routines::unexpected" we can ignore that
// rest of the exceptions are thrown regardless
if(str_contains($exception->getMessage(), 'SSL_read')){
Log::warning($exception->getMessage(), [
'stacktrace' => $exception->getTraceAsString(),
'file' => $path
]);
restore_error_handler();
return;
}
restore_error_handler();
throw $exception;
}
}
}
@DominikStyp
Copy link
Author

Purpose of this, was to get rid of the annoying error "ftp_fput(): SSL_read on shutdown: error:0A000126:SSL routines::unexpected eof while reading in vendor/league/flysystem-ftp/FtpAdapter.php on line 164" which is caused by incompatible SSL/TSL versions between the FTP Server and OpenSLL for PHP. As of OpenSSL >= 3.0 this happens for old FTP servers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment