laravel12.laravel_112
最終投稿日:2026年06月22日
C:\~\laravel_12> composer require league/flysystem-ftp "^3.0"
./composer.json has been updated
Running composer update league/flysystem-ftp
Loading composer repositories with package information
Updating dependencies
Lock file operations: 1 install, 0 updates, 0 removals
~ 省略 ~
82 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force
[37;44m INFO [39;49m No publishable resources for tag [1m[laravel-assets] [22m.
Found 1 security vulnerability advisory affecting 1 package.
Run "composer audit" for a full list of advisories.
C:\~\laravel_12>
return [
~ 省略 ~
'disks' => [
'ftp' => [
'driver' => 'ftp',
'host' => env('FTP_HOST'),
'username' => env('FTP_USERNAME'),
'password' => env('FTP_PASSWORD'),
'port' => (int)env('FTP_PORT', 21),
'root' => env('FTP_ROOT', '/'),
'passive' => true,
'ignorePassiveAddress' => true,
'ssl' => false,
'timeout' => 30,
],
~ 省略 ~
use Illuminate\Support\Facades\Storage;
~ 省略 ~
public function index(Request $request) {
$path = Storage::disk('local')->path('ftp/hoge.txt');
Storage::disk('ftp')->putFileAs('/', $path, 'hoge.txt');
ファイルではなく、文字列を送信する場合は『put』関数もお手軽です。
Storage::disk('ftp')->put('test.txt', 'テスト内容');
use Illuminate\Support\Facades\Storage;
~ 省略 ~
public function index(Request $request) {
$path = Storage::disk('local')->path('ftp/hoge.txt');
$contents = Storage::disk('ftp')->get('FtpDir/hoge.txt');
file_put_contents($path, $contents);
use Illuminate\Support\Facades\Storage;
~ 省略 ~
public function index(Request $request) {
$files = Storage::disk('ftp')->files('/');
$dirs = Storage::disk('ftp')->allDirectories('/');
[ファイル][ディレクトリ]は同時に参照できないの?
実はできますが、取得結果が少し複雑です。
使用する関数は『listContents』ですが、返却はオブジェクトとなります。
配列で取得したい場合は、以下の様にしてください。
$alls = Storage::disk('ftp')->listContents('/')->toArray();
$files = array_filter($alls, fn($i) => $i['type'] === 'file');
$dirs = array_filter($alls, fn($i) => $i['type'] === 'dir');
configでのFTP設定に疑問を持たれた方はいませんか?
鋭い!
『何故パッシブの通信に関する設定が2つあるの?』ですよね。
確かに以下の様に2つ設定しています。
・'passive' => true,
・'ignorePassiveAddress' => true,
後者の設定は実は、Laravelの【クセ】を回避する為に必要だったりします。
前者の設定は当然『通信はパッシブだよ』を知らせます。
基本的にFTPはLinux環境のvsftpdと通信すると思いますが、これが若干曲者で、環境によって[内部IP][ローカルIP][IPv6]などを返す場合があります。
それに対し、Laravelが素直に通信をしようとするとコケる訳です。
よって先ほどの設定【ignorePassiveAddress】の出番です。
この設定は先方からの返却された通信をガン無視して『実際に接続しているIP』を使います。