Answer the question
In order to leave comments, you need to log in
How to multi-upload images in Laravel 5?
Good day to all!
I'm doing a multi-upload of images, but for some reason I can't upload all the images. Let's say I select 3 images, and 2. MB are loaded. Can anyone tell me how to work with multi-upload? Also, when multiloading, in validation, I can't use checking for image and mime types...
Controller :
$this->validate($request, [
'title_news' => 'required|min:3|unique:news',
'short_news' => 'required|min:5',
'full_news' => 'required|min:5,',
'preview_news' => 'required|image', // Не работает image и mime
]);
$commentsModerator = Auth::user()->role->news_role['premoderator_role']; // Премодерация
$addNews = News::create([
'title_news' => $request->title_news,
'short_news' => $request->short_news,
'full_news' => $request->full_news,
'author_news' => $userID,
'commentsModerator_news' => $commentsModerator
]);
if($addNews){
foreach($request->file('preview_news') as $file){
// Оригинальное изображение
$fileNameOriginal[] = $addNews['id_news'] . '-' . time() . '-original.jpg'; // 1-1502110636-original.jpg
$uploadOriginal = Image::make($file)->save(public_path() . '/uploads/posts/news/preview/original/' . $addNews['id_news'] . '-' . time() . '-original.jpg');
// 200x200 изображение
$fileName200[] = $addNews['id_news'] . '-' . time() . '-200.jpg'; // 1-1502110636-200.jpg
$uploadOriginal200 = Image::make($file)->resize(200, 200)->save(public_path() . '/uploads/posts/news/preview/200/' . $addNews['id_news'] . '-' . time() . '-200.jpg');
// 50x50 изображение
$fileName50[] = $addNews['id_news'] . '-' . time() . '-50.jpg'; // 1-1502110636-50.jpg
$uploadOriginal50 = Image::make($file)->resize(50, 50)->save(public_path() . '/uploads/posts/news/preview/50/' . $addNews['id_news'] . '-' . time() . '-50.jpg');
}
$addPreview = Preview::create([
'idUser_preview' => $userID,
'idNews_preview' => $addNews['id_news'],
'original_preview' => json_encode($fileNameOriginal),
'x200_preview' => json_encode($fileName200),
'x50_preview' => json_encode($fileName50)
]);
return redirect()->route('home')->with('successMessages', 'Вы успешно добавили новость!');
}else{
return redirect()->route('home')->with('warningMessages', 'Ошибка добавлении новости!');
}
<div class="panel-body">
{{ Form::open(['route' => ['News.postAddNews', Auth::user()->id_users], 'files' => true, 'enctype' => 'multipart/form-data']) }}
<div class="form-group {{ ($errors->first('title_news')) ? 'has-error' : '' }}">
<label for="title_news" class="control-label">Заголовок новости:</label>
<input type="text" class="form-control" name="title_news" placeholder="{{ ($errors->first('title_news')) }}">
</div>
<div class="form-group {{ ($errors->first('short_news')) ? 'has-error' : '' }}">
<label for="short_news" class="control-label">Короткая новость:</label>
{{ Form::textarea('short_news', '', ['class' => 'form-control', 'placeholder' => $errors->first('short_news')]) }}
</div>
<div class="form-group {{ ($errors->first('full_news')) ? 'has-error' : '' }}">
<label for="full_news" class="control-label">Текст новости:</label>
{{ Form::textarea('full_news', '', ['class' => 'form-control', 'placeholder' => $errors->first('full_news')]) }}
</div>
<div class="form-group {{ ($errors->first('preview_news')) ? 'has-error' : '' }}">
<label for="preview_news">Превью картинка</label>
<input type="file" name="preview_news[]" multiple="true" accept="image/jpeg,image/png,image/jpg">
<p class="help-block">{{ $errors->first('preview_news') }}</p>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-plus"></i> Добавить</button>
</div>
{{ Form::close() }}
</div>
Answer the question
In order to leave comments, you need to log in
Validation
You have the following variables in foreach: $
fileNameOriginal
[] = ...
$fileName200[] = ....
It's not clear why []
you select 3 pictures, they are loaded, but there are only two in $request->file('preview_news')?
$fileName... contains "generated" file names from all uploaded files, which are then entered into the database as json. And I did "[]" to create an array and fit data into them. It seems there is a better way, but I studied old school)
At the expense of pictures. I chose, say, 3 pictures, but only 2 were saved to the desired directory, and somewhere 1. In $request, all pictures are shown, it turns out that saving pictures does not work correctly.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question