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
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'm trying to store base64Image with laravel but it returns this error:
Can't write image data to path (/home/u187504358/domains/example.com/public/images/Group-1590235658.jpg)
Code
if ($request->photo) {
$photo = $request->photo;
$filename = 'Group' . '-' . time() . '.' . 'jpg';
$location = public_path('images/'. $filename);
Image::make(file_get_contents($photo))->resize(500, 500)->save($location);
$oldFilename = $group->photo;
if(!empty($group->photo)){
Storage::delete($oldFilename);
}
$group->photo = $filename;
}
filesystem.php
'default' => env('FILESYSTEM_DRIVER', 'local'),
'disks' => [
'local' => [
'driver' => 'local',
'root' => public_path('images/'),
],
]
Any idea?
Solved
I've added this code to my index.php file in public aka public_html folder and it works now
$app->bind('path.public', function() {
return __DIR__;
});
Note:
As you see in error it tries to connect with folder public (domains/example.com/public/images) but in server (shared host) there is public_html folder. So it made me think twice that laravel tries to connect to the wrong path and code above solved the issue.
-Hope it help others.
I'm working on a Laravel project, and I make a feature that the user can upload image for its profile, and I use the public path to save the avatars cause 000webhost doesn't support (storage:link) and every thing works better in local
but when I upload the project on 000webhost, it refuses to upload the image and returns error (The image failed to upload.)
How can i solve it
config/filesystem.php
'public' => [
'driver' => 'local',
'root' => public_path('storage2'),
'url' => env('APP_URL').'/storage2',
'visibility' => 'public',
],
controller
public function Change(Request $request)
{
$Validate = $this->Validation($request);
if (!$Validate->fails()) {
$User = $this->getUser();
$OldImage = $User->image;
$Extension = $request->file("image")->getClientOriginalExtension();
$NewImage = str_random(30) . "." . $Extension;
Storage::putFileAs($this->privatePath, $request->file("image"), $NewImage);
$User->update(["image" => $NewImage]);
Storage::delete($this->privatePath ."/". $OldImage);
session()->flash("message", "You have changed your image");
return back();
} else {
session()->flash("error", $Validate->errors()->first());
return back();
}
}
But the problem is not in the code, cause it works in local
I think the problem with some permissions or in file .htaccess or anything like that
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 need to upload a file in my Laravel eCommerce app and store it inside the respective user's folder. My code looks like this:
if ($request->hasFile('attach')) {
try {
$file = $request->file('attach');
$filename = $file->getClientOriginalName();
Storage::disk('local')->put($filename, File::get($file));
Storage::move('app/'.$filename, 'app/public/uploads/'.$user_id.'/attach/'.$filename);
I've this in config/filesystems.php:
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
But I get error while uploading a file:
(1/1) FileNotFoundException: File not found at path: app/Test1.docx
How can I store/save a file in a custom location such as user's folder? The custom location where uploaded files are stored look like this:
'app/public/uploads/'.$user_id.'/attach/'
As advised by #sohel0415, here is the updated code that worked for me:
if ($request->hasFile('attach')) {
try {
$file = $request->file('attach');
$filename = $file->getClientOriginalName();
Storage::disk('local')->put('public/uploads/'.$user_id.'/attach/'.$filename, File::get($file));