03_2_3 file upload - hpscript/laravel GitHub Wiki
<form action="/uploadfile" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<input type="file" class="form-control-file" name="fileToUpload" id="exampleInputFile">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
collectiveのForm::openのthird parameterに'files'=>trueを追加
Form:file('file')とする
{!! Form::open(['method'=>'POST', 'action'=>'PostsController@store', 'files'=>true]) !!}
{{ csrf_field()}}
<div class="form-group">
{!! Form::file('file', ['class'=>'form-controll']) !!}
</div>
<div class="form-group">
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', null, ['class'=>'form-controll']) !!}
</div>
<div class="form-group">
{!! Form::submit('Create Post', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
public function store(CreatePostRequest $request)
{
return $request->file('file');
}
オリジナル名、ファイルサイズ
public function store(CreatePostRequest $request)
{
$file = $request->file('file');
echo "<br>";
echo $file->getClientOriginalName();
echo "<br>";
echo $file->getClientSize();
}
$ php artisan make:migration add_path_column_to_posts --table=posts
migration file
public function up()
{
Schema::table('posts', function (Blueprint $table) {
//
$table->string('path');
});
}
public function down()
{
Schema::table('posts', function (Blueprint $table) {
//
$talbe->dropColumn('path');
});
}
$ php artisan migrate
Post.php
protected $fillable = [
'user_id',
'title',
'content',
'path'
];
PostsController.php
public function store(CreatePostRequest $request)
{
$input = $request->all();
if($file = $request->file('file')){
$name = $file->getClientOriginalName();
$file->move('./images', $name);
$input['path'] = $name;
}
Post::create($input);
}
view
<ul>
@foreach($posts as $post)
<div class-"image-container">
<img height="100" src="images/{{$post->path}}">
</div>
<li><a href="{{ route('posts.show', $post->id) }}">{{$post->title}}</a></li>
@endforeach
</ul>
images/{{$post->path}} は、accessorsでimages/を省略する
Model:Post.php
public $directory = "/images/";
public function getPathAttribute($value){
return $this->directory . $value;
}
View: index.php
<div class-"image-container">
<img height="100" src="{{$post->path}}">
</div>
Laravelでは画像はstorageフォルダに格納する
Webからのアクセスを許すには、public/storageからstorage/app/publicへシンボリックリンクを張る必要がある
https://readouble.com/laravel/6.x/ja/filesystem.html
### 駄目な例 moveコマンドで、public配下に格納
UsersController.php
if($file = $request->file('file')){
$name = $file->getClientOriginalName();
$file->move('./images/tmp/', $name);
$inputs['path'] = $name;
$ php artisan storage:link
// ./public/storageが./storage/app/publicへのリンクとなる
UsersController.php
if($file = $request->file('file')){
$name = $file->getClientOriginalName();
// $file->move('./images/tmp/', $name);
$file->storeAs('./public/images/tmp/', $name);
$inputs['path'] = $name;
confirm.blade.php
<img src="{{ $inputs['path'] ? asset('/storage/images/tmp/' . $inputs['path']) : 'https://placehold.jp/100x100.png' }}" class="img-icon">
確認画面で戻るボタンが押された場合は、Storage::deleteで削除する。確認画面で登録完了ボタンが押された場合は、画像の前部にCarbonでtimestampを付けて、prdフォルダに移動させる。画像のpathはDBに格納する。
use Carbon\Carbon;
use Illuminate\Support\Str;
$action = $request->get('action');
$inputs = $request->except('action');
if($action == '戻る'){
Storage::delete('public/images/tmp/'.$inputs['profile_img']);
return redirect()->action('UsersController@create')->withInput($inputs);
}
$timestamp = Carbon::now()->timestamp;
$path = $timestamp.'_'.$inputs['profile_img'];
Storage::move('public/images/tmp/'.$inputs['profile_img'], 'public/images/prd/'.$path);
return 'done';