Laravel Validator / Uploaded File Fails At Required - validation

"dd" output of Input::all() in postController:
array(8) {
["_token"]=>
string(40) "6WZ87M1LCiVCsaUS9HbjZckRibXfF2RP69LCpW7K",
...
...
["svg"]=>
object(Symfony\Component\HttpFoundation\File\UploadedFile)#9 (7) {
["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
bool(false)
["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(39) "Screenshot from 2013-06-18 17:07:27.png"
["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
string(9) "image/png"
["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(29747)
["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>
int(0)
["pathName":"SplFileInfo":private]=>
string(14) "/tmp/phpdRTDU7"
["fileName":"SplFileInfo":private]=>
string(9) "phpdRTDU7"
}
}
Validation:
$rules = array('svg' => 'required');
$check = Validator::make(Input::except('_token'), $rules);
if($check->fails()){
return Redirect::back()->withErrors($check);
}else{
return Redirect::back()->with('message', 'No problem');
}
And I get the error message:
Error message:
The svg field is required.
Even if I upload file as you see on dd output, it shows that error always.
Thanks,

user2413500 found that the problem was using Input::except('_token') which did not include the file object. However, Input::all() does include the file object. This seems to be a bug which I'll report, but the definition of Input::except is "all" minus the items you don't want.
But what seems to be happening is "all" minus the items you don't want, minus your file!
Itrulia and Taylor say this is not a bug.
However, these are confusingly not identical statements when you have a $_FILE posted...
$params = Input::except('_token'); // Missing file inputs!
$params = array_except(Input::all(), '_token'); // The current solution.
Be on guard! :)

Related

How to get an attribute from a relation in Laravel?

This is part of my code:
$form = new Form(new Shop());
$form->tab('terminal', function (Form $form) use ($id) {
$form->hasMany('shopterminal', '', function (Form\NestedForm $form) {
$form->text('terminal_num', 'terminal number')->required();
$form->select('poz_type', 'POS type')->options(['static' => 'one', 'dynamic' => 'two'])->required();
$form->select('psp_id', 'POZ name')->options(Psp::pluck('name', 'id'))->required();
$form->text('sheba', 'sheba number');
$form->text('account_num', 'account number')->required();
$form->select('bank_id', 'bank name')->options(Bank::pluck('name', 'id'))->required();
dd($form);
});
Here is the result of dd($form):
I need to get the value of terminal_image item (which is 15841949062134.png). Any idea how can I get it?
Noted that, neither of below syntax works:
$form->get('terminal_image')
$form->select('terminal_image')
$form->terminal_image
$form()->terminal_image
$form->relation->terminal_image
For your specific example:
$form->form->model->shopterminal[0]->terminal_image
you can use :
$form->form->model->relations['shopterminal']->items[0]->attributes['terminal_image']

Mime type validation not working in Laravel 5.7

I want to validate a .pfx file but it fails all the time.
I tried the following code:
$validacoes = [
'certificado' => 'mimetypes:application/x-pkcs12'
];
but it doesn't work.
if I do
dd($request->file('certificado')->getMimeType())
I get: application/x-pkcs12.
what can be wrong?
For images it does work: image/jpg, image/png, etc.
try this
$validacoes = [
'certificado' => 'mimes:application/x-pkcs12'
];
Here's how it should be according to the docs:
'certificado' => 'mimes:p12,pfx'
I have the exact same problem.
My file:
"certificateFile" => UploadedFile {#5824
-test: false
-originalName: "file.pfx"
-mimeType: "application/x-pkcs12"
-error: 0
#hashName: null
My validation rules:
public function rules()
{
return [
'certificateFile' => 'required|mimetypes:application/x-pkcs12',
'certificatePassword' => 'required'
];
}
Any ideas guys?
Edit: Ok, found an answer in another question (Laravel 5 Mime validation) that might answer our problem.
In this comment (https://stackoverflow.com/a/30023227/2796516) it is explained that Laravel uses the guessExtension to validate our file. And checking the source code of said extension we find:
'application/x-pkcs12' => 'p12',
Which probably means that it only accepts certificate files with the p12 extension, pfx wont work.

Laravel - "file does not exist or is not readable", but the file is moved successfully

I get the follow error:
The "C:\xampp\tmp\php49D8.tmp" file does not exist or is not readable.
But the file is copied successfully
My controller code is:
$fileResult=$file->move(self::UPLOAD_DIR, $name_file);
if(!$fileResult){
$result = array("status" => "500",
"error"=> array("error" => "Error in the file move"));
return response(json_encode( $result ), $result["status"])
->header("Content-Type", "application/json");
}
Screenshot: here
Why can be the problem?
Call $validator->fails() can delete uploading file
use
$file = $request->file('file');//get your file
$fileResult=$file->move(self::UPLOAD_DIR, $file->getClientOriginalName());

Laravel 5.3 response()->download - File(.doc, .docx) becomes unreadable after downloading

Laravel 5.3
When I download a file (.doc, .docx) from my storage folder it becomes unreadable. If I go to the local folder and open the file however it is valid and readable.
I am using the standard download function, using headers and stuff.. Have a look at my code:
$fileNameGenerate = 'example_filename';
$fileArr = [ 'wierd_filename', 'docx' ];
$cvPath = storage_path('app/example_folder/subfolder/wierd_filename.docx');
$headers = array(
'Content-Type: application/' . $fileArr[1],
);
try {
return response()->download($cvPath, $fileNameGenerate . '.' . $fileArr[1], $headers);
} catch (\Exception $e) {
//Error
return redirect()->back()->with('error', trans('locale.file_does_not_exists'));
}
Does anyone know what is wrong here? Thank you!
Update: I removed headers, it doesn't work with or without them.
Here is how the files render in the 2 different cases:
Try this
public function getDownload()
{
//doc file is stored under storagepath/download/info.docx
$file= pathofstorage. "/download/info.docx";
return response()->download($file);
}
I added:
ob_end_clean();
before:
response -> download
and it worked for me.

New Caller Insert to Database with Codeigniter using the Twilio API

Below is the function to receive all incoming calls in my Controller
public function call_incoming()
{
$blocklist = $this->call_log_model->get_blocklist($_REQUEST['From']);
$tenantNum = $this->call_log_model->get_called_tenant($_REQUEST['From']);
$tenantInfoByNumber = $this->account_model->getTenantInfoByNumber($tenantNum->to_tenant);
$officeStatus = $this->check_office_hours($tenantInfoByNumber->start_office_hours, $tenantInfoByNumber->end_office_hours);
$calldisposition = $this->calldisp_model->get_call_disposition($tenantInfoByNumber->user_id);
$response = new Services_Twilio_Twiml;
if($blocklist == 0)
{
if($officeStatus == "open")
{
if($_POST['Called'] != AGENTPOOL_NUM)
{
$data = array(
'caller'=>$_REQUEST['From'],
'to_tenant'=>$_POST['Called'],
'date_created'=>date('Y-m-d H:i:s')
);
$this->call_log_model->insert_caller_to_tenant($data);
$dial = $response->dial(NULL, array('callerId' => $_REQUEST['From']));
$dial->number(AGENTPOOL_NUM);
print $response;
}
else
{
$gather = $response->gather(array('numDigits' => 1, 'action'=>HTTP_BASE_URL.'agent/call_controls/call_incoming_pressed', 'timeout'=>'5' , 'method'=>'POST'));
$ctr = 1;
foreach($calldisposition as $val )
{
$gather->say('To go to '.$val->disposition_name.', press '.$ctr, array('voice' => 'alice'));
$gather->pause("");
$ctr++;
}
print $response;
}
}
else
{
$response->say('Thank you for calling. Please be advise that our office hours is from '.$tenantInfoByNumber->start_office_hours.' to '.$tenantInfoByNumber->end_office_hours);
$response->hangup();
print $response;
}
}
else
{
$response->say('This number is blocked. Goodbye!');
$response->hangup();
print $response;
}
}
Please advise if I need to post the model...
Here is whats happening everytime an unknown number calls in, the caller will hear an application error has occurred error message and when checking the Twilio console the error it is giving me is
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: agent/Call_controls.php
Line Number: 357
Please be advised that this error only occurs when the caller is a number not in our database yet. When the call comes from a number already saved in our databse, this codes works...
Thank you for the help...
if($tenantNum) {
$tenantInfoByNumber = $this->account_model->getTenantInfoByNumber($tenantNum->to_t‌​enant);
} else {
$tenantInfoByNumber = ""; // fill this in with relevant fill data
}
This should fix your issue, as there is no TenantNum returned, there is no data, so make it yourself for unknown numbers.

Resources