Always show [The post image must be a file of type: jpg, jpeg, png, webp.] laravel - laravel

It always shows error in validation. I tried uploading png jpeg jpg and all showed error.
View
<form action="{{ route('post.store') }}" method="POST" enctype="multipart/form-data">
<div id="post_image_container">
<input type="file" name="post_image[]" id="post_image" class="bg bg-light border rounded text-dark font w-100 p-3 form-control" placeholder="Add Images" accept="image/*" multiple>
#if($errors->first('post_image'))
<small class="form-text d-block text-danger fw-bold">{{ $errors->first('post_image') }}
</small>
#endif
</div>
Controller - Validation
$this->validate($request, [
'post_title' => ['required', 'min:5'],
'post_tags' => ['required', 'min:5'],
'post_content' => ['required', 'min:50'],
'post_image' => ['required', 'mimes:jpg,jpeg,png,webp']
]);
dd($request->all());

You are uploading multiple images, therefore, you need to state that in your validating by writing this:
$this->validate($request, [
'post_title' => ['required', 'min:5'],
'post_tags' => ['required', 'min:5'],
'post_content' => ['required', 'min:50'],
'post_image' => ['required', 'array'],
'post_image.*' => ['required', 'mimes:jpg,jpeg,png,webp'],
]);

Related

laravel 9 has defferent location then i set, and every file i put become .tmp

i dont understand why, i do it in laravel 8 and it work but not in laravel 9
Store function
public function store(Request $request)
{
$ValidatedData = $request->validate([
'title' => 'required|max:255',
'slug' => 'required|unique:menus',
'categories_id' => 'required',
'deskripsi' => 'required',
'is_order' => 'required',
'most' => 'required',
'price' => 'required',
'image' => 'image|file|max:1024'
]);
if($request->file('image')){
$validatedData['image'] = $request->image->store('menus');
}
$ValidatedData['users_id'] = Auth::user()->id;
$ValidatedData['excerpt'] = Str::limit(strip_tags($request->deskripsi), 100);
Menus::create($ValidatedData);
return Redirect('/dashboard/menus')->with('success', 'Tugas Baru Telah Ditambahkan!');
}
Form
<div class="mb-3">
<label for="image" class="form-label">Size Max. 1mb</label>
<input class="form-control #error('image') is-invalid #enderror" type="file"
name="image" id="image">
#error('image')
<div class="invalid-feedback">
{{ $message }}
</div>
#enderror
</div>
.env
FILESYSTEM_DISK=public
After Create the storage location is change

Laravel - Validation Message not showing on blade

I'm having trouble on showing the validation error messages. I tried the other similar questions but doesn't work for me. Please help me.
This is my Controller.blade.php
public function store(Request $request)
{
$this->validate($request,[
'surname'=>['required', 'string', 'max:255'],
'firstname'=>['required', 'string', 'max:255'],
'birthday'=>'required',
'pofbirth'=>['required', 'string', 'max:255'],
'gender'=>['required', 'numeric'],
'address'=>['required', 'string', 'max:255'],
'city'=>['required', 'string', 'max:255'],
'district'=>['required', 'string', 'max:255'],
'cap'=>['required', 'string', 'max:255'],
'cstatus'=>['required', 'string', 'max:255'],
'conjugated'=>['required', 'string', 'max:255'],
'cellphone'=>['required', 'numeric'],
'email'=>['required', 'string', 'email', 'max:255'],
]);
This is my message.blade.php
#if (count($errors) > 0)
#foreach ($errors->all() as $error)
<p class="alert alert-danger">{{ $error }}</p>
#endforeach
#endif
#if (session()->has('message'))
<p class="alert alert-success">{{ session('message') }}</p>
#endif
This is my blade.php
#include('includes.navbar')
<div class="col" style="background-color: #ffffff;">
#include('includes.message')
<form method="POST" action="{{ route('app.store')}}" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="container shadow" style="background-color: rgb(248,249,250);">
<div class="row">
<div class="col">
...
...
...
Thank you so much!
In your method, you have no any session key named message , but there are no any key named message in your validation :
#if (session()->has('message'))
<p class="alert alert-success">{{ session('message') }}</p>
#endif
It would be firstname, surname, gender, age etc (which key you are using in validation). Like this :
#if($errors->has('firstname'))
<p class="alert alert-success">{{ $errors->first('firstname') }}</p>
#endif
Get all error message :
#if ($errors->any())
#foreach ($errors->all() as $error)
<div>{{$error}}</div>
#endforeach
#endif
Change your validation in this way :
$validatedData = $request->validate([
'surname' => 'required|string|max:255',
'firstname' => 'required|string|max:255',
'birthday' => 'required',
'pofbirth' => 'required|string|max:255',
'gender' => 'required|numeric',
'address' => 'required|string|max:255',
'city' => 'required|string|max:255',
'district' => 'required|string|max:255',
'cap' => 'required|string|max:255',
'cstatus' => 'required|string|max:255',
'conjugated' => 'required|string|max:255',
'cellphone' => 'required|numeric',
'email' => 'required|string|email|max:255',
]);

The image failed to upload.on a server laravel

The image failed to upload.
link
https://comedoruniversitariouncp.000webhostapp.com/products/create
The project works in local server,
the error appears when i upload to a server
create.blade.php
<form action="{{ route('products.store') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="form-group row">
<label class="col-form-label col-sm-2">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="name">
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-sm-2">Price</label>
<div class="col-sm-10">
<input type="number" class="form-control" name="price" step="0.1">
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-sm-2">Amount</label>
<div class="col-sm-10">
<input type="number" class="form-control" name="amount" >
</div>
</div>
<div class="form-group row">
<label class="col-form-label col-sm-2">Image</label>
<div class="col-sm-10">
<input type="file" class="form-control-file" name="image">
</div>
</div>
<button type="submit" class="btn btn-primary">Create</button>
</form>
ProductController.php
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'price' => 'required',
'amount' => 'required',
'image' => 'required|image'
]);
$image = $request->file('image');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
Product::create([
'name' => $request->name,
'price' => $request->price,
'amount' => $request->amount,
'image' => $new_name
]);
return redirect()->route('products.index')->with('message', 'Product created successfully');
}
As you mention about works on local but not remote. I assumed that the upload_max_filesize is greater than the size your upload file, and both on local and remote are not the same.
You may use The Storage Facade as a convenient way to interact with your local filesystems.
use Illuminate\Support\Facades\Storage;
//...
$new_name = rand() . '.' . $image->getClientOriginalExtension();
Storage::disk('public')->putFileAs('images', request->file('image'), $new_name);
//...
Docs
You should try this
Try with adding mimetypes in validation of image.
public function store(Request $request)
{
$request->validate([
'name' => 'required',
'price' => 'required',
'amount' => 'required',
'image' => 'required|mimes:jpeg,bmp,png'
]);
$image = $request->file('image');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
Product::create([
'name' => $request->name,
'price' => $request->price,
'amount' => $request->amount,
'image' => $new_name
]);
return redirect()->route('products.index')->with('message', 'Product created successfully');
}
You Should Try code this,
you may change a part code :
$image->move(public_path('images'), $new_name);
to be code :
$image->move(public_path('images'.$new_name));
this is code, 100% work to me.

Laravel 5.7.17 file validation issue

The example is based on Laravel's registration.
I have added following to register.blade.php:
<div class="form-group row">
<label for="file" class="col-md-4 col-form-label text-md-right">{{ __('Files') }}</label>
<div class="col-md-6">
<input type="file" id="files" name="files[]" multiple>
</div>
</div>
The method in RegisterController looks like this:
protected function validator(array $data)
{
$validator = Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'files.*' => ['required', 'file'],
]);
dd($validator->errors());
}
I'm trying to upload a PDF and a DOC file.:
MessageBag {#236 ▼
#messages: array:2 [▼
"files.0" => array:1 [▼
0 => "The files.0 must be a file."
]
"files.1" => array:1 [▼
0 => "The files.1 must be a file."
]
]
#format: ":message"
}
Must be a file? These are files...
just add enctype="multipart/form-data" to your form:
<form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
Try this :
protected function validator(array $data)
{
$validator = Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'files.*' => ['required', 'mimes:doc,docx,pdf,txt'],
]);
dd($validator->errors());
}

why not update the picture? laravel 5.4

I can upload a picture, but when I try to update it, then there is no error. The file name changes in the database to the name of the new picture. And in the public folder the picture remains old and does not appear new.
I use: Intervention Image
What I did not understand, help please.
Controller: UploadController
use Illuminate\Http\Request;
use App\Post;
use Image;
use Storage;
use Faker\Provider\File;
public function update (Request $request, $id)
{
//validate
$this->validate($request, [
'title' => 'required|max:255',
'author' => 'required',
'text' => 'required',
'desc' => 'required',
'image' => 'required',
]);
$posts = Post::find($id);
$posts->title = $request->input('title');
$posts->author = $request->input('author');
$posts->text = $request->input('text');
$posts->desc = $request->input('desc');
$posts->image = $request->input('image');
//update image
if ($request->hasFile('image'))
{
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('images/'. $filename);
Image::make($image)->resize(800, 400)->save($location);
$oldFilename = $posts->image;
//update db
$posts->image = $filename;
//delete old image
Storage::delete($oldFilename);
}
$posts->save();
return redirect('/');
}
View: edit.blade.php
<div class="container">
<form method="POST" action="{{ route('goUpdate', [$posts->id]) }}">
{{ csrf_field() }}
{!! method_field('patch') !!}
#if($posts)
<div class="form-group">
<br>
<label>title</label>
<input name="title" type="text" class="form-control" value="{{ $posts->title }}">
</div>
<div class="form-group">
<label>author</label>
<input name="author" type="text" class="form-control" value="{{ $posts->author }}">
</div>
<div class="form-group">
<label>text</label>
<textarea name="text" class="form-control" rows="7">{{ $posts->text }}</textarea>
</div>
<div class="form-group">
<label>desc</label>
<textarea name="desc" class="form-control" rows="5">{{ $posts->desc }}</textarea>
</div>
<div class="form-group">
<label>image</label>
<input type="file" name="image" class="form-control-file" value="" >
</div>
<div>
<input name="submit" type="submit" class="btn btn-primary" value="update"/>
back
</div>
#endif
</form>
<br>
filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => public_path('images/'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
],
Try using this
$image = $request->file('image');
$filename = time() . '.' . $image->getClientOriginalName();
$path = public_path('events/' . $filename);
Image::make($image->getRealPath())->resize(300, 300)->save($path);
$event->image = 'events/' . $filename;
Hope this can help you

Resources