laravel upload Can't write image data to path - php

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.

Related

Uploading image in 000webhost don't work using laravel framework

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

Get / Read laravel 5.8 Storage non public Folder files to View?

Try to access 'storage/app/folder1/a.png' from my view
public function viewStorageFiles()
{
$fileFullPath = Storage::disk('local')->path('folder1/a.png');
$fileUrl = Storage::disk('local')->url('app/folder1/a.png');
$storage_path = storage_path('app/folder1/a.png');
return view('email.fileDownload')->with([
'fileFullPath' => $fileFullPath,
'fileUrl' => $fileUrl,
'storage_path' => $storage_path,
]);
}
In view : email.fileDownload
<div>
<p> asset($fileUrl) ==> {{asset($fileUrl)}}</p>
<img src="{{asset($fileUrl)}}"/>
</div>
<div>
<p> url($fileUrl) ==> {{url($fileUrl)}}</p>
<img src="{{url($fileUrl)}}"/>
</div>
<div>
<p> storage_path($fileUrl) ==> {{storage_path($fileUrl)}}</p>
<img src="{{storage_path($fileUrl)}}"/>
</div>
the result is :
There could be many answers to this!
You could create a symbolic link from "public/storage" to "storage/app/public" using the following command:
php artisan storage:link
Above command will map your storage/app/public directory to public directory.
Now let's consider you have user1.jpg and user2.jpg files in your storage/app/public directory, you can access them in the following way:
http://your-domain.com/storage/user1.jpg
http://your-domain.com/storage/user2.jpg
* Updated my answer based on your comment: *
You can return a file response from a route that is protected by some middleware!
For example - following route returns file response from storage/app/uploads directory that is not accessible publicly:
Route::get('storage/{file}', function ($file) {
$path = storage_path('app' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $file);
return response()->file($path);
});
You could secure above route in anyway and use it in your views..
I hope that helped..
Go to config/filesystems add this array
'public_site' => [
'driver' => 'local',
'root' => public_path('storage'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
]
then your function could be like this
public function viewStorageFiles()
{
$fileFullPath = Storage::disk('public_site')->path('folder1/a.png');
$fileUrl = Storage::disk('public_site')->url('app/folder1/a.png');
$public_path = public_path('storage/app/folder1/a.png');
return view('email.fileDownload')->with([
'fileFullPath' => $fileFullPath,
'fileUrl' => $fileUrl,
'storage_path' => $public_path,
]);
}

laravel doesn't upload images on server

I can upload my images while i'm working on local but when I moved my site to host it doesn't upload images while get names of them in database,
my filesystems.php
'default' => env('FILESYSTEM_DRIVER', 'local'),
disks' => [
'local' => [
'driver' => 'local',
'root' => public_path('images/'),
],
....
sample of my upload codes:
if ($request->hasFile('imageOne')) {
$imageOne = $request->file('imageOne');
$filename = 'productone' . '-' . time() . '.' . $imageOne->getClientOriginalExtension();
$location = public_path('images/');
$request->file('imageOne')->move($location, $filename);
$product->imageOne = $filename;
}
Issue:
In host i get uploaded image name in database, but no image in my
public/images folder.
any idea?
UPDATE
I found interesting folder in my host, as I moved all my public folder files to server public_html now it created another folder in my root called public and inside that has images folder including all images i uploaded so far!
Questions:
Why laravel created new public/images folder in my root instead of
using my public_html?
How to fix it?
SOLVED
I added this code to my public_html/index.php file and it's working now.
// set the public path to this directory
$app->bind('path.public', function() {
return __DIR__;
});
Hope it helps.
I change the public path inside my save image controller to look like this:
if (request()->imagefile->move(public_path('../../public_html/destination'), $imageName))
echo "Yap";
else echo "Nop";

How to store files in custom location

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));

Laravel store uploaded image path with filename

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

Categories