I am working on a Laravel project and I have routes set up for a form page where it shows the form on GET and it analyzes it on POST:
Route::get('/update-data', [
'as' => 'user.settings.edit-data',
'uses' => 'UserController#editData',
]);
Route::post('/update-data', [
'as' => 'user.settings.update-data',
'uses' => 'UserController#updateData',
]);
In this form I ask the user to fill out two fields with text and I also ask them to upload two files. Both files must be jpeg, png or pdf. In the controller I have:
$this->validate($request,
[
'phone' => 'required',
'email' => 'required|email',
'file1' => 'required|mimes:jpeg,png,pdf',
'file2' => 'required|mimes:jpeg,png,pdf',
]);
If that succeeds, then the code will continue executing and save everything, but if not it will redirect the user back to the form. Is there a way to still have the file chosen so that the user doesn't need to look for it again?
I would suggest validating using server side code (not javascript, although both can be nice from the user perspective (but obviously don't rely on the js validation).
I usually validate like this in Laravel (I'm not the biggest fan of their built in validator). Then after the validation you can use Laravel's File class to get the file name and re-post the data to the view:
$file = Request::file('file1');
$accepts = ['jpeg', 'png', 'pdf'];
$ext = $file->getClientOriginalExtension();
$filename = $file->getClientOriginalName();
if( !in_array($ext, $accepts) )
{
return view('your-view')->withErrors('Invalid File Format');
}
else
{
//File has one of the correct extensions,
//return the filename to the view so it can be
//re-displayed in the input
return view('your-view', ['filename' => $filename]);
}
Or if you prefer the non-facade way:
$file = $request->file('file1');
$accepts = ['jpeg', 'png', 'pdf'];
$ext = $file->getClientOriginalExtension();
$filename = $file->getClientOriginalName();
if( !in_array($ext, $accepts) )
{
return view('your-view')->withErrors('Invalid File Format');
}
else
{
//File has one of the correct extensions,
//return the filename to the view so it can be
//re-displayed in the input
return view('your-view', ['filename' => $filename]);
}
This takes advantage of Laravels File class.
Related
I realize this question has been asked several times on this forum, but none of the solutions given has resolved my particular issue.
I get the above error when trying to update my DB.
Here is a snippet of my code
I have tried adding findorfail on my request by id
$service=Service::findorfail($request->input('id'));
This returns a 404 page.
ServiceController.php
public function updateService(Request $request){
// return $request->all();
$this->validate($request, ['serviceTitle'=> 'required',
'serviceSubTitle'=> 'required',
'description'=> 'required',
'slug' => 'required|min:3|max:255|unique:services',
'serviceImage'=>'image|nullable|max:1999']);
$service=Service::find($request->input('id'));
$service->title =$request->input('serviceTitle');
$service->sub_title =$request->input('serviceSubTitle');
$service->slug =$request->input('slug');
$service->description =$request->input('description');
if($request->hasFile('serviceImage')) {
//1 : get filename with ext
$fileNameWithExt = $request->file('serviceImage')->getClientOriginalName();
//2 : get just file name
$fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
//3 : get just extension
$extension = $request->file('serviceImage')->getClientOriginalExtension();
//4: filename to store
$fileNameToStore = $fileName.'_'.time().'.'.$extension;
//upload image
$path =$request->file('serviceImage')->storeAs('public/serviceImage', $fileNameToStore);
$old_image =Service::find($request->input('id'));
if($old_image!='noimage.jpg') {
Storage::delete('public/serviceImage/'.$old_image->image);
}
$service->image =$fileNameToStore;
}
$service->update();
return redirect('/services')->with('status', 'The '.$service->title.' Service has been updated successfully');
}
The error log tells me the error is on this specific line:
$service->title =$request->input('serviceTitle');
Thank you for time and assistance.
I use this yii2 module for an image uploading purposes.
My upload folder is:
C:\XAMPP\HTDOCS\MYAPPNAME\WEB\UPLOAD
├───cache
├───global
└───store
└───Products
└───Product15
Where store is the folder for original pictures and the cache is for the resized ones. yii2-images is configured like this:
'yii2images' => [
'class' => 'rico\yii2images\Module',
//be sure, that permissions ok
//if you cant avoid permission errors you have to create "images" folder in web root manually and set 777 permissions
'imagesStorePath' => 'upload/store', //path to origin images
'imagesCachePath' => 'upload/cache', //path to resized copies
'graphicsLibrary' => 'GD', //but really its better to use 'Imagick'
'placeHolderPath' => '#webroot/upload/store/no-image.png', // if you want to get placeholder when image not exists, string will be processed by Yii::getAlias
],
and my Product model upload method is:
public function upload()
{
if ($this->validate()) {
$path = 'upload/store/' . $this->image->baseName . '.' . $this->image->extension;
$this->image->saveAs($path);
$this->attachImage($path);
#unlink($path);
return true;
} else {
return false;
}
}
Uploading images work. I mean, they fisically appear where they should. I load image
<?php $img = $model->getImage(); ?>
and in my DetailView widget instead of 'img':
[
'attribute' => 'image',
'value' => "<img src='{$img->getUrl()}'>",
'format' => 'html',
],
And what I get instead of a rendered image is:
image-by-item-and-alias?item=Product15&dirtyAlias=71273544a6-1.jpg
I have no idea where this "image-by-item-and-aliasitem=Product15&dirtyAlias?" part even comes from when it actually must be something like:
Products\Product15\71273544a6-1.jpg
and given format html parameter it must therefore render an image.
Can you please pinpoint me to the source of this error?
I'm trying to figure out what is wrong with my validation, but I'm not sure.
I have created a file upload that uploads the file to S3. Works fine except when I need to validate python files.
In my FileUploadController.php I have a store(FileStoreRequest $request) method that handles the upload. I added the $validatedData = $request->validate(); in it and it works.
I have also added the mimes.php in config folder with the following:
<?php
return [
'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),
'py' => array('text/plain', 'application/x-python' , 'application/octet-stream, application/x-python-code, text/x-python-script', 'text/x-python'),
];
And the rules() method inside my FileStoreRequest class is
public function rules()
{
return [
'preprocessor' => 'mimes:py',
];
}
Any time I try to upload the python file I get the error
The preprocessor must be a file of type: py.
When I remove the mimes check from the rules() it passes.
The rules work, because I tested it on another view for zip file upload.
Any ideas what could be wrong?
You can create custom validation like:
$input = $request->all();
if (isset($input["preprocessor"]) && !empty($input["preprocessor"])) {
$filesource = $input["preprocessor"];
$fileExtension = $filesource->getClientOriginalExtension();
$input["ext"] = $fileExtension;
}
$rules = array(
'ext' => 'nullable|in:py',
);
I have a laravel upload for file (along with other data that is passed to the database) Everything works. But I just can't figure out how to save the path of the file that is saved.
Here is my controller function:
public function store(Request $request)
{
request()->validate([
'name' => 'required',
'logo' => 'nullable',
'original_filename' => 'nullable',
]);
//This is where the file uploads?
if ($request->hasFile('logo')) {
$request->file('logo')->store('carrier_logo');
$request->merge([
'logo' => '',//TODO: get file location
'original_filename' => $request->file('logo')->getClientOriginalName(),
]);
}
Carrier::create($request->all());
return redirect()->route('carriers.index')->with('toast', 'Carrier created successfully.');
}
The thing I want to achieve:
I want logo to fill with something like carrier_logo/ZbCG0lnDkUiN690KEFpLrNcn2exPTB8mUdFDwAKN.png
The thing that happened every time I tried to fix it was that it placed the temp path in the database. Which ended up being something in the PHP install directory.
Just assign result to variable:
$path = $request->file('logo')->store('carrier_logo');
According to docs
Then you can do with $path variable whatever you want.
just assign the value like this.
$location=base_path('img/'.$filename);
and save it in db.
You could do this:
For FileName
$fileName = $request->file('test')->getClientOriginalName();
OR
$fileName = $request->user()->id.'.'.$request->file('logo')->getClientOriginalExtension();
$imageDirectory = 'logo_images';
$path = $request->file('logo')->storeAs($imageDirectory, $fileName);
dd($path);
I am using Laravel 5.2 for my project. I need some help to validate the file upload. Functionality is like below,
fileType - audio/video file
file.mp3, file.mp4, file.3gb
So while submitting(Rest Client through PostMan) the above fields i need to validate the file for "audio extension(.mp3, etc)" if fileType is"audio".
Same way i need to validate the file for video extension if fileType is "video(.mp4,.3gb)"
I tried to do that but there is an option to validate audio/video extension without checking the value of fileType as below,
'file' => 'mimes:mp3,mp4,3gb'
Can anyone guide me on this?
If you are using form requests, try with below code
public function rules()
{
$rules = [
'some_field' => 'required',
];
// if fileType is audio
if ($this->input('fileType') == 'audio') {
$rules['file'] = 'mimes:mp3,mp4';
}
//if fileType is video
if ($this->input('fileType') == 'video') {
$rules['file'] = 'mimes:mp4,3gp';
}
return $rules;
}
Change the field names and validation rules as per your requirement.
You can provide following validation rule for mp3
$validation_args = [
'file' => 'required|mimes:mpeg',
];