laravel validation doesnt work for .JPG - laravel-5

In Laravel5 I validate a picture with the following code:
public function rules() {
return [
'name' => 'required',
'pic' => 'required|mimes:jpeg,png',
];
}
It works for files with extensions filename.jpg and filename.jpeg but it doesn't for filename.JPG.
However, it doesn't return wrong file extension but a missing file. Can anyone help?

Try this in your pic rules:
'pic' => 'required|mimes:jpeg,jpg,png',
Explanation
Had this problem and found that when you use mimes: rule, you have to specify the extensions you want as valid i. e. you have to add both jpeg and jpg extension variations.
Other solutions
Use mimetypes: rule with image/jpeg that covers 3 extension variations for the jpeg format: jpg jpeg jpe.
Use image rule which covers jpeg, png, bmp, gif, or svg including jpeg's extension variations
More on the mimes and mimetypes rules: https://laravel.com/docs/5.5/validation#rule-mimetypes
All MIME types:
A full listing of MIME types and their corresponding extensions may be found at the following location: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

Try 'pic' => 'required|image' and make sure your form has the enctype="multipart/form-data" attribute.

Related

laravel response()->download return empty body for non-image formats like pdf, zip and so on

I want to download a file. File formats can be mp3, mp4, pdf, JPG, png and so on. The problem is that image format works properly and returns a correct file, but other file returns empty body and I can't download them.
My code is: (pdf format)
$filePath = storage_path( 'messages/'. $message->message_file );
if( file_exists($filePath) ){
$headers = array(
'Content-Type' => 'application/pdf',
);
return response()->download($filePath, '', $headers);
Please help me, thanks.

Laravel 5.8.17 - validation image not working as expected

I'm creating Laravel validation, and meet strange issue.
I have a form with input type file, where I can attach multiple files.
<input type="file" name="img[]" class="form-control" multiple="multiple">
In the Controller I have validation rules:
$validationRules = [
'img' => 'required|image'
];
And all the time my validation doesn't work, I always get a message:
"The img must be an image."
There is no difference when I upload 1 file or 2 or 3 at the same time - error is the same. There are another fields at the same form (text fields) and all is ok with them, problem appears only with image. Tried to remove "image"
from the validation and replace it with mimetypes or mimes - anyway system can't recognize image.
Please advice what could be a problem here, thanks a lot.
You need to make validation attribute img to img.*
$input_data = $request->all();
$validator = Validator::make(
$input_data, [
'img.*' => 'required|mimes:jpg,jpeg,png,bmp|max:20000'
],[
'img.*.required' => 'Please upload an image',
'img.*.mimes' => 'Only jpeg,png and bmp images are allowed',
'img.*.max' => 'Sorry! Maximum allowed size for an image is 20MB',
]
);

How to send blade as attachment in pdf format in laravel?

I want to send blade file as attachment in mail with laravel. I am
writing below code for this but its not working.
Below is my controller where i am getting data from form and then send
this data to another controller where my attachment function is
called.
$data = array('shareholders'=>$request->com_shareholder_count,'contract_send'=>$request->contract_send);
$to = $mail_log->to_email_id = $request->email_id;
$mail = Mail::to($to)->send(new SendMailable($data));
This is my SendMailable controller :
$director_info_pdf = view('directors_info',compact('data'))->render();
On return this variable it shows me error :
message: "Invalid view.", exception: "InvalidArgumentException",…}
exception: "InvalidArgumentException"
file: "C:\xampp\htdocs\caps_admin\vendor\laravel\framework\src\Illuminate\Mail\Mailer.php"
line: 285
message: "Invalid view."
After this line i am writing code to attach my files. where i am sending some files other 3 files are directly send from folder. And last one is attached from blade file.
->attachdata($director_info_pdf, 'dynamic_data.pdf')
->attach( $public_path.'/'.'contract.pdf', [
'as' => 'contract.pdf',
'mime' => 'application/pdf',
])
->attach($public_path.'/'.'HMRC.pdf',[
'as' => 'HMRC.pdf',
'mime' => 'application/pdf',
])
->attach($public_path.'/'.'clientR3.pdf',[
'as' => 'contract1.pdf',
'mime' => 'application/pdf',
]);
I am able to send mail with all 4 files as attachment. But when i am trying to open my files in mail rest 3 files are working as pdf. but ->attachdata($director_info_pdf, 'dynamic_data.pdf') this file get corrupted.
I dont know how to first change this file into pdf and then send as attachment.
I am using snappy for pdf.
I think you want to send pdf as an attachment in your email without saving it in your system .
your can refer the following code to do this .
public function sendmail()
{
$data["email"]='useremail#gmail.com';
$data["client_name"]='user_name';
$data["subject"]='sending pdf as attachment';
//Generating PDF with all the post details
$pdf = PDF::loadView('userdata'); // this view file will be converted to PDF
//Sending Mail to the corresponding user
Mail::send([], $data, function($message)use($data,$pdf) {
$message->to($data["email"], $data["client_name"])
->subject($data["subject"])
->attachData($pdf->output(), 'Your_Post_Detail.pdf', [
'mime' => 'application/pdf',
])
->setBody('Hi, welcome user! this is the body of the mail');
});
}
hope it helped you .
for better understanding you can follow this article
how to send pdf as an attachment in email in laravel
I dont know how to first change this file into pdf and then send as attachment. I am using snappy for pdf.
So that's the problem right there. You are attaching an HTML file, and simply naming it as if it's a PDF. That won't work. You do indeed need to generate this PDF from the HTML view that you render.
Here is the documentation for snappy:
https://github.com/barryvdh/laravel-snappy#usage
I suggest you to read up on it, as the examples given there are pretty straight forward and easy to understand.
This is untested, but if you've set up Snappy correctly, based on your code, something like this should work:
$snappy = App::make('snappy.pdf');
$director_info_pdf = $snappy->getOutputFromHtml(view('directors_info',compact('data'))->render());

Laravel 4 imagecache intervention not displaying images

Here's the product page with an imagecached image: http://kmk-prof.kz/product/dx700lc
As you can see - image is not being displayed and has following url: /imagecache/catalogue/dx700lc-541a82fe99ac3.jpg
But when i try to browse that image just in my uploads folder - http://kmk-prof.kz/uploads/dx700lc-541a82fe99ac3.jpg it displays just fine.
Why could imagecache not work?
Here's the imagecache config.php:
'route' => 'imagecache',
'paths' => array(
public_path('uploads'),
),
'templates' => array(
'catalogue' => function($image) {
return $image->fit(213, 213);
},
),
It's very hard to tell you why the image is not displaying without any code.
Looking at the site, your image (in the html) is looking for a file in /imagecache/catalogue/dx700lc-541a82fe99ac3.jpg when your file is in uploads/dx700lc-541a82fe99ac3.jpg.
you can do 2 things:
change the code to load the file from uploads/dx700lc-541a82fe99ac3.jpg.
or, change your image path to /imagecache/catalogue/dx700lc-541a82fe99ac3.jpg
if you want more help, please post your code.
PS. your IMAGE URL link doesn't work ^_^

yii unsecure attribute while using mimetypes file validation

i use the following mime type file validation
array('image', 'file', 'allowEmpty' => true,'mimeTypes'=>'image/gif, image/jpeg, image/jpg, image/png', 'on' => 'update')
works well if a file doesnt have the right mime type.
but if the validation is successful, the logg sais
2012/09/27 17:13:02 [warning] [application] unsafe Attribut "image" couldnt be set.
but it is defintily declared as save
array('image', 'safe', 'on' => 'update')
thats why i receive an error like this
finfo_file(C:\xampp\tmp\php3729.tmp): failed to open stream: No such file or directory
$mimeType=finfo_file($info,$file->getTempName());
seems, that the file cant be saved i its unsafe attribute.
the strange thing is, if i uncomment the validation rule with mimeTypes check, everthing works fine.
in my controller i set the image file like this
if (isset($_POST['User'])) {
$user->attributes = $_POST['User'];
$user->image = CUploadedFile::getInstance($user, 'image');
if ($user->validate()) {
I believe you run $user->image->saveAs($path) before saving the whole model, that's why it's trying to validate your file in temporary folder when it is already moved to the $path.
So, use $user->validate() or run saveAs($path) after saving model.

Resources