ホームページ >バックエンド開発 >PHPチュートリアル >ストレージを使用してLaravelでファイルのダウンロードを発見してください::ダウンロード
Storage::download
を使用する例です。
Storage::download()
<?php namespace App\Http\Controllers; use Illuminate\Support\Facades\Storage; class FileController extends Controller { public function download($filename) { return Storage::download( "documents/{$filename}", "custom-{$filename}", ['Content-Type' => 'application/pdf'] ); } }
全体として、
基礎となるストレージプロバイダーの詳細を隠しながら、ファイルをサービスするための安全で効率的な方法を提供します。<?php namespace App\Http\Controllers; use App\Models\Document; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; class DocumentController extends Controller { public function download(Request $request, Document $document) { if (!$request->user()->canDownload($document)) { abort(403); } if (!Storage::exists($document->path)) { abort(404, 'File not found'); } $document->increment('download_count'); return Storage::download( $document->path, $document->original_name, [ 'Content-Type' => $document->mime_type, 'Content-Disposition' => 'attachment', 'Cache-Control' => 'no-cache, must-revalidate' ] ); } }
以上がストレージを使用してLaravelでファイルのダウンロードを発見してください::ダウンロードの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。