zahid hasan emon
zahid hasan emon

Reputation: 6233

Image Upload is not working in a Laravel Project hosted in a shared server

I have deployed a Laravel project in a shared hosting. I have changed my .env file and copied all files from the public folder to the main directory and deleted the public folder. Now the problem is, whenever I am trying to upload an image, I am getting an internal server error. I suppose the problem is the Image Intervention is not getting the right folder to save the image. I have tried the both ways given below:

if ($request->hasfile('admin_pro_pic')) {  
    $image = $request->file('admin_pro_pic');
    $filename  = time() . '.' . $image->getClientOriginalExtension();
    $location = public_path('/images/admin/' . $filename);
    Image::make($image)->resize(950, 700)->save($location);
    $admin->admin_pro_pic = $filename;               
}   

and

if ($request->hasfile('admin_pro_pic')) {  
    $image = $request->file('admin_pro_pic');
    $filename  = time() . '.' . $image->getClientOriginalExtension();
    $location = '/images/admin/' . $filename;
    Image::make($image)->resize(950, 700)->save($location);
    $admin->admin_pro_pic = $filename;               
}

But None of these is working. Any possible Solution?

Upvotes: 0

Views: 3975

Answers (3)

Gabrielle-M
Gabrielle-M

Reputation: 1077

Try This.

use Storage;
use File;
if(!empty($request->file('admin_pro_pic')))
    {
        $file = $request->file('admin_pro_pic') ;
        $fileName = $file->getClientOriginalName() ;
        $destinationPath = public_path().'/images/' ;
        $file->move($destinationPath,$fileName);
       
        $admin->image=$fileName;
    }

Create imges inside public directory.

Upvotes: 1

Faraz Irfan
Faraz Irfan

Reputation: 1326

Use laravel base_path function, so your code will look like this

if ($request->hasfile('admin_pro_pic')) {  
    $image = $request->file('admin_pro_pic');
    $filename  = time() . '.' . $image->getClientOriginalExtension();
    $location = base_path().'/images/admin/' . $filename;
    Image::make($image)->resize(950, 700)->save($location);
    $admin->admin_pro_pic = $filename;               
}

Answer Update

Issue was fileinfo extension missing or disbaled.

Upvotes: 2

Michael W. Czechowski
Michael W. Czechowski

Reputation: 3455

I am handling it like this:

// check for defined upload folder inside .env file, otherwise use 'public'
$publicUploadDir = env('UPLOAD_PUBLIC', 'public/');
// get file from request
$image = $request->file('admin_pro_pic');
// hasing is not necessary, but recommended
$new['path'] = hash('sha256', time());
$new['folder] = 'images/admin/';
$new['extension'] = $file->extension();
// store uploaded file and retrieve path
$image->storeAs($publicUploadDir, implode($new, '.'));

Upvotes: 0

Related Questions