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);
Related
I am Try to Upload User Avatar with Intervention Image as below private function
Everything working fine but image name save to database with my specific folder name
eg : user_avatar/image.jpg
Here is My Code Thanks everyone
private function storeAvatar($user)
{
if (request()->hasFile('avatar')) {
$user->update([
'avatar' => request()->avatar->store('user_avatar', 'public'),
]);
$avatar = Image::make(public_path('storage/' .$user->avatar))->fit(300, 300);
$avatar->save();
}
}
enter image description here
You're not setting a file name.
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
As indicated here: https://laravel.com/docs/7.x/filesystem#file-uploads
So your line should be:
$user->update([
'avatar' => request()->avatar->storeAs('user_avatar', 'filename', 'public'),
]);
Or something along those lines. You can also use the file name as uploaded by making the filename the same as the original.
$filename = $request->file('avatar')->getClientOriginalName();
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 want to store the uploaded users image's path in db.Here is my code in controller,
$file = Input::file('pic');
$img = Image::make($file);
Response::make($img->encode('jpeg'));
$filepath = $file->getPathName();
But the above code results,
wrong path like C:\xampp\tmp\phpA0C8.tmpBt I've uploaded image from my laravel public folder.I've tried plenty of codes but they doesn't work.How do I solve this?Can anybody help?
Use laravel file store method and hashname
public function fileUPload(Request $request){
$file = $request->file('pic');
$dir = '/my_images/';
$file->store($dir, 'public_path'); //store images
echo $filePath= $dir . $file->hashName();
}
//config/filesystems.php
return [
///....
'disks' => [
'public_path' => [
'driver' => 'local',
'root' => public_path(),
],
//...
],
];
You have to define a path with the file name.
$FileName = Input::file('pic')->getClientOriginalName();
$FileDestinationPath = public_path('') .'/folder-name-in-public-folder/'.$FileName;
Or you can get it by using store method
$path = Input::file('pic')->store('folder-name');
return $path;
https://laravel.com/docs/5.5/filesystem
Use the store method
$filepath = $request->file('pic')->store('pictures');
more info
Place the code before the response
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.