FTP 使用 Laravel 7 上传文件时出现连接问题
FTP connection issue while upload a file using Laravel 7
我想使用 FTP
将文件从 Laravel
上传到另一台服务器。
看起来很简单,让我们来看看我的配置:
.env 文件
FTP_HOST=dl.myserver.com
FTP_USERNAME=beni@dl.myserver.com
FTP_PASSWORD=somePass
filesystem.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'ftp' => [
'driver' => 'ftp',
'host' => env('FTP_HOST'),
'username' => env('FTP_USERNAME'),
'password' => env('FTP_PASSWORD'),
'passive' => true,
'port' => 21,
'root' => '/home/myserver/public_html/podcasts'
],
.
.
.
和我的控制器终于
$year = Carbon::now()->year;
$month = Carbon::now()->month;
$day = Carbon::now()->day;
//podcast
$podcast = $request->file('podcast');
$filename = $podcast->getClientOriginalName();
$purename = substr($filename, 0, strrpos($filename, '.'));
$filenametostore = $purename . '_' . $year .'_' . $month . '_' . $day . '.' . $podcast->getClientOriginalExtension();
Storage::disk('ftp')->put($filenametostore, fopen($request->file('podcast'), 'r+'));
.
.
.
但是我有这个错误:
League\Flysystem\ConnectionRuntimeException
Could not log in with connection: dl.myserver.com::21, username:
beni@dl.myserver.com
我的 FTP
帐户和信息是真实的,因为我使用 FileZilla
.
登录
顺便提一下,我的 dl.server.com
使用的是 CPANEL
。
关于这个问题有什么想法吗?
提前致谢
令人惊讶的是,当我将 env('FTP_HOST')
、env('FTP_USERNAME')
和 env('FTP_PASSWORD')
替换为 filesystems.php
[ 中的等效字符串值时,问题就解决了=28=] 文件!
我尝试了纯 PHP FTP functions
并弄明白了:
$conn_id = ftp_connect("dl.myserver.com");
ftp_login($conn_id, "beni@dl.myserver.com", "somePass");
dd(ftp_put($conn_id, $filenametostore, $request->file('podcast'), FTP_ASCII));
所以我的 Laravel filesystem.php
看起来像这样:
'ftp' => [
'driver' => 'ftp',
'host' => "dl.myserver.com", //env('FTP_HOST'),
'username' => "beni@dl.myserver.com", //env('FTP_USERNAME'),
'password' => "somePass", //env('FTP_PASSWORD'),
],
在我的情况下效果很好。
您需要在您的 .env 中将密码用双引号引起来
特别是如果您的密码包含空格或#
FTP_PASSWORD="一些#通过"
我想使用 FTP
将文件从 Laravel
上传到另一台服务器。
看起来很简单,让我们来看看我的配置:
.env 文件
FTP_HOST=dl.myserver.com
FTP_USERNAME=beni@dl.myserver.com
FTP_PASSWORD=somePass
filesystem.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'ftp' => [
'driver' => 'ftp',
'host' => env('FTP_HOST'),
'username' => env('FTP_USERNAME'),
'password' => env('FTP_PASSWORD'),
'passive' => true,
'port' => 21,
'root' => '/home/myserver/public_html/podcasts'
],
.
.
.
和我的控制器终于
$year = Carbon::now()->year;
$month = Carbon::now()->month;
$day = Carbon::now()->day;
//podcast
$podcast = $request->file('podcast');
$filename = $podcast->getClientOriginalName();
$purename = substr($filename, 0, strrpos($filename, '.'));
$filenametostore = $purename . '_' . $year .'_' . $month . '_' . $day . '.' . $podcast->getClientOriginalExtension();
Storage::disk('ftp')->put($filenametostore, fopen($request->file('podcast'), 'r+'));
.
.
.
但是我有这个错误:
League\Flysystem\ConnectionRuntimeException
Could not log in with connection: dl.myserver.com::21, username: beni@dl.myserver.com
我的 FTP
帐户和信息是真实的,因为我使用 FileZilla
.
顺便提一下,我的 dl.server.com
使用的是 CPANEL
。
关于这个问题有什么想法吗?
提前致谢
令人惊讶的是,当我将 env('FTP_HOST')
、env('FTP_USERNAME')
和 env('FTP_PASSWORD')
替换为 filesystems.php
[ 中的等效字符串值时,问题就解决了=28=] 文件!
我尝试了纯 PHP FTP functions
并弄明白了:
$conn_id = ftp_connect("dl.myserver.com");
ftp_login($conn_id, "beni@dl.myserver.com", "somePass");
dd(ftp_put($conn_id, $filenametostore, $request->file('podcast'), FTP_ASCII));
所以我的 Laravel filesystem.php
看起来像这样:
'ftp' => [
'driver' => 'ftp',
'host' => "dl.myserver.com", //env('FTP_HOST'),
'username' => "beni@dl.myserver.com", //env('FTP_USERNAME'),
'password' => "somePass", //env('FTP_PASSWORD'),
],
在我的情况下效果很好。
您需要在您的 .env 中将密码用双引号引起来 特别是如果您的密码包含空格或#
FTP_PASSWORD="一些#通过"