laravel12.laravel_103
最終投稿日:2026年3月17日
C:\~\laravel_12>php artisan storage:link
INFO The [C:\~\laravel_12\laravel_12\storage] link has been connected to [C:\~\storage\app/public].
C:\~\laravel_12>
use Illuminate\Support\Facades\Storage;
~ 省略 ~
public function index(Request $request) {
$content = Storage::get('files\hoge.txt');
Storage::put('hoge/foo.txt', $content);
use Illuminate\Http\File as FileInfo;
~ 省略 ~
// 対象ファイルのフルパス設定;
$path = resource_path();
$full_path = $path . '/sql/users_name.sql';
// ファイル存在確認
if (file_exists($full_path)) {
// ファイル情報取得
$file = new FileInfo($full_path);
// フルパス取得
$hoge = $file->getPathname();
// ファイル名(拡張子含む)取得
$hoge = $file->getFilename();
// 拡張子取得
$hoge = $file->getExtension();
// MIMEタイプ取得
$hoge = $file->getMimeType();
// ファイルサイズ(バイト)取得
$hoge = $file->getSize();
// 実際の絶対パス取得
$hoge = $file->getRealPath();
}
use Illuminate\Support\Facades\File;
~ 省略 ~
// 対象ファイルのフルパス設定
$path = resource_path();
$full_path = $path . '/sql/hoge.txt';
// ファイルを書き出す
File::put($full_path, '内容');
// ファイルを存在確認して削除する
if (File::exists($full_path)) {
File::delete($full_path);
}
// コピー
File::copy($full_path, $path . '/sql/new.txt');
// 移動
File::move($full_path, $path . '/sql/new.txt');
// 追記
File::append($full_path, '追記内容');
public function upload(Request $request) {
if ($request->method() == 'GET') {
return view('hoge/upload', []);
} elseif($request->method() == 'POST') {
//
}
}
<html>
<head>
<title>アップロード画面</title>
</head>
<body>
{{-- バリデーションエラー表示 --}}
@if ($errors->any())
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
<form method="POST" action="upload" enctype="multipart/form-data">
@csrf
<input type="file" name="file">
<BR>
<button type="submit">アップロード</button>
</form>
</body>
</html>
public function upload(Request $request) {
if ($request->method() == 'GET') {
return view('hoge/upload', []);
} elseif($request->method() == 'POST') {
// バリデーション
$request->validate([
'file' => 'required|max:2048',
]);
// ファイルオブジェクト取得
$file_info = $request->file('file');
}
}
| $file_info->getClientOriginalName() | アップロードしたファイル名 |
| $file_info->getClientOriginalExtension() | アップロードしたファイルの拡張子 |
| $file_info->getMimeType() | アップロードしたファイルMIMEタイプ(例:image/jpeg) |
| $file_info->getSize() | アップロードしたファイルサイズ(byte) |
| $file_info->getPathname() | アップロードしたファイルのテンポラリーパス |
| $file_info->getError() | エラーコード(正常 = 0) |
「$file_info->store($path)」の指定だとデフォルトディスクへ保存となりイマイチ何処に保存されるか視認性が悪い。
なので同じ[storage]ディレクトリへ保存するなら、「Storage」クラスの方が便利です。
例えば以下の様に記述できます。
Storage::disk('local')->putFileAs('hoge', $request->file('file'), 'hoge.jpg');
これは「~\storage\app\private\hoge\hoge.jpg」へ保存する事を意味します。
public function upload(Request $request) {
if ($request->method() == 'GET') {
return view('hoge/upload', []);
} elseif($request->method() == 'POST') {
// バリデーション
$request->validate([
'file' => 'required|max:2048',
]);
$file_info = $request->file('file');
$file_name = 'hoge.jpg';
// 保存
$file_info->move(resource_path(), $file_name);
// ZIPパス(ファイル名付き)作成
$zipPath = resource_path() . '/hoge.zip';
// クラス初期化
$zip = new \ZipArchive();
if ($zip->open($zipPath, \ZipArchive::CREATE) === TRUE) {
$zip->addFile(resource_path() . '/' . $file_name, $file_name);
$zip->close();
}
}
}
use Illuminate\Support\Facades\Storage;
~ 省略 ~
public function index(Request $request) {
$datas = [];
$datas[] = ['hoge' => 'ほげ1', 'foo' => 'ふ~1', 'bar' => 'ばぁ1'];
$datas[] = ['hoge' => 'ほげ2', 'foo' => 'ふ~2', 'bar' => 'ばぁ2'];
$datas[] = ['hoge' => 'ほげ3', 'foo' => 'ふ~3', 'bar' => 'ばぁ3'];
$file_name = '/hoge.csv';
$path = Storage::disk('local')->path('csv');
$fp = fopen($path . $file_name, 'w');
foreach ($datas as $data) {
$line = implode(',', $this->addDoubleQuote($data));
$line = mb_convert_encoding($line, "SJIS-win", "UTF-8");
fwrite($fp, $line . "\n");
}
fclose($fp);
}
// 値を「"」で囲う
private function addDoubleQuote($arr = []) {
$quoted = array_map(function($value) {
return '"' . $value . '"';
}, $arr);
return $quoted;
}
public function index(Request $request) {
$datas = [];
$datas[] = ['hoge' => 'ほげ1', 'foo' => 'ふ~1', 'bar' => 'ばぁ1'];
$datas[] = ['hoge' => 'ほげ2', 'foo' => 'ふ~2', 'bar' => 'ばぁ2'];
$datas[] = ['hoge' => 'ほげ3', 'foo' => 'ふ~3', 'bar' => 'ばぁ3'];
$file_name = 'hoge.csv';
$handle = fopen('php://output', 'w');
// UTF-8からShift-JIS変換(日本語対応)
stream_filter_prepend($handle, 'convert.iconv.utf-8/cp932//TRANSLIT');
foreach ($datas as $data) {
fputcsv($handle, $data);
}
fclose($handle);
return response()->streamDownload(function () {
}, $file_name, [
'Content-Type' => 'application/octet-stream',
]
);
}
Laravel ver.12から「Storage」クラスが参照するルートディレクトリが変更となりました。
【Ver.12以降】~\storage\app\private
~\storage\app\public
※ ルートを変更したい場合は「\config\filesystems.php」で、連想配列《local》を編集します。