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

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

Related

laravel upload Can't write image data to path

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.

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 5.4 uploading on cpanel, Laravel Mediable works on local host but have path issue in cpanel

We are using laravel mediable for joining image to model in our web, Code is given below. We have default images in a folder and use Laravel package Intervention for uploading.
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$id = $user->id;
$uname = $user->name[0];
$img = Image::make('uploads/users/default/'.$uname.'.png');
$img->save('uploads/users/images/'.$id.'.png');
$media = MediaUploader::import('uploads', 'users/images', $id, 'png');
$user->attachMedia($media, 'user');
$img = Image::make('uploads/users/default/'.'badge1'.'.png');
$img->save('uploads/users/images/'.$id.'badge'.'.png');
$media = MediaUploader::import('uploads', 'users/images', $id.'badge', 'png');
$user->attachMedia($media, 'badge');
}
But on cpanel path error occurs, Intervention uploads the file on exact location but MediaUploader of Laravel Mediable unable to find file from path to attach it to the user. Can someone help? i hope i have defined the issue correct.

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