Laravel 5.4 upload multiple images with validation rules - validation

I'm using following code to upload multiple images but I need to add some validation rules like file should be an image with mentioned extensions and file size. Currently it upload everything.
This is my html view code:
<input required="" type="file" name="photos[]" id="photos" multiple="" directory="" webkitdirectory="" mozdirectory="">
This is my controller code:
foreach ($request->photos as $photo) {
$filename = $photo->store('photos');
$data['photoName'] = $filename;
PhotosModel::SavePhotos($data);
$message = "Photos Added Successfully";
}
I've added below code inside foreach loop and before that loop to but it's giving error that photos must be an image and of mentioned types.
$this->validate($request, [
'photos' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048'
]);
I want to upload all images and stop other files from being uploaded. Thanks!

Try this (before your foreach):
$this->validate($request, [
'photos' => 'required',
'photos.*' => 'image|mimes:jpeg,png,jpg,gif|max:2048'
]);

Related

The edit.blade in Laravel has a submit button for editing changes and updating, but it updated without any changes

Laravel's edit blade has an image tag to insert the image and updates the changes using a button in the form. There is a remove button in img tag that allows to remove and replace an image.
but the image is updated without any changes, and the uploaded image remains blank without any changes that occur on that particular page.
public function update(Request $request, $id)
{
//dd ($request[img_name]);
$updateData = $request->validate([
'img_name' => '',
'name' => 'required|max:255',
'url' => 'required|max:255',
'description' => '|max:255',
]);
if(!isset ($updateData['img_name'])){
$updateData['img_name']='../../img/links/blank_img_link-370x65-min.png';
}
Info_links::whereId($id)->update($updateData);
return redirect('/account/info_links/index')->with('completed', 'Informative link has been updated');
}
I expect it to submit using its submit button without any changes to the edit blade. 

Send attachment with Laravel SMTP and save to local storage

I read a similar topic in the forum solved, but it did not work successfully in me. Where am I making a mistake?
View
<input type="file" class="" name="document">
Send
Mail::send([], [], function ($message) use ($request) {
$message->to($request->to);
$message->subject($request->subject);
$message->setBody($request->message);
$data = $request->document;
$message->attach($data['document']->getRealPath(), array(
'as' => $data['document']->getClientOriginalName(),
'mime' => $data['document']->getMimeType()));
I want to send the attachment in this way, but it does not. Do I need to upload and then send it first?
There is a whole section about files in requests at official laravel's docs.
https://laravel.com/docs/7.x/requests#files
https://laravel.com/api/7.x/Illuminate/Http/UploadedFile.html
So, according to the above docs, the file you upload is (temporarly) saved when processing the request - you do not need to save it yourself.
$document_path = $request->document->path();
$message->attach($document_path, array(
'as' => $request->document->getClientOriginalName(),
'mime' => $request->document->getMimeType())
);

laravel 5 validation custom error message not appear

I want to show the custom validation error message if user upload an image size of more than 4 MB. However, once submitted with an image of like above 4MB, it shows the default error message : "The file name failed to upload.". Below is my code in the controller:
$messages = [
'fileName' => 'Image maximum size exceed. ',
];
$validator = Validator::make($request->all(), [
'fileName' => 'max:4096',
], $messages);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator->errors());
}
Here is the HTML code in the blade file:
<input type="file" name="fileName">
I know this is an old question but I have to post this answer here. This was what worked for me
$messages = [
'fileName.uploaded' => 'Image maximum size exceed. ',
];
I used this line:
return redirect()->back()->withErrors($validator->customMessages);
and it solved the problem.

How to send ajax response like download button to download files Laravel

I want to display download link button on my view (if my ajax is success)
follwings is my download code to send ajax response and dispaly on view
<div class="blog-moder-button">
<a href="public_path('files').'/'.$request->file_name" class="button-md dark-button downlds">
Download PDF
</a>
</div>
following is my controller
public function
downloadform(Request $request) {
$validator = Validator::make($request->all(),[
'name' => 'required',
'email' => 'required|email',
'file_name' => 'required',
]
);
if ($validator->passes()) {
$msg= array();
// $msg['success'] = '<div class="alert alert-success"> Successfully Registered</div>';//it is working
$msg['success'] = '<div class="blog-moder-button"> Download PDF</div>';//it is not working
}
return response()->json($msg);
}
Do not render html inside your controller, that is the job of your view.
To trigger a file download response use this:
return response()->download($pathToFile);
https://laravel.com/docs/5.6/responses#file-downloads

backpack for laravel segment missing during upload

I created a CRUD (using backpack crud for laravel )for uploading certificates, when i upload the pdf I use
$this->crud->addField([
'name' => 'respaldo',
'label' => 'Respaldo',
'type' => 'upload',
'upload' => true,
'wrapperAttributes' => [
'class' => 'form-group col-md-6'
],
]);
when i want to see the link of the uploaded file, storage is missing from the route so i added it to a mutator ,see below
public function setRespaldoAttribute($value)
{
$attribute_name = "respaldo";
$disk = "public";
$destination_path = "uploads/respaldos";
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
}
public function getRespaldoLink() {
return '<a href="'.asset('storage/'.$this->respaldo).'" target="_blank">
Descargar</a>';
}
Now when clicking on the link it's displayed on the listview, the problem is when i click on edit , "storage" segment is missing there so i get 404 instead. see image
I got the same issue and tried to amend the link before displaying it on browser for user to preview in Update or Show CRUD.
The issue is whatever the path is stored inside your database, it will pop up on browser without the head folder. As we know, we can amend the head folder with Laravel LIKE asset('storage/'.$file_path)
So, you need to update the path before storing into database.
Search and Update file HasUploadFields.php
Update this line with add-on 'storage/' Like this:
$this->attributes[$attribute_name] = 'storage/' . $file_path;
Do same for uploadMultipleFilesToDisk method
Now for all new file uploaded, it will add-on 'storage/' before stored file path

Resources