Search code examples
laraveluploadcpanelmovefilepath

Laravel 5.x - Problem with load image and upload or moving file on shared hosting


I've been following this thread

But, so far it is not enough to solve my problems, I want to ask the best practice or solution to be applied for this problem

this is my controller code:

  if($request->hasfile('news_photo_url'))
    {

           $image = $request->file('news_photo_url');
           $name=$image->getClientOriginalName();
           $image_path = public_path().'\\img\\news';
           $image_path_to_db = '\\img\\news\\'.$name;
           $image->move($image_path,$name);
    }

and this is how I load image,

<img src="{{ asset($news->news_photo_url) }}" alt="">

and this is perfectly fine on my local,

but things is different in shared hosting cpanel, my app is located here

home/laravel/

and my public (index.php) is located here

home/public_html/name/

how to work this out properly ?

since add this line of code also didn't work,

$app->bind('path.public', function() {return __DIR__; });

since this simply tells the public is here, but the path stored in db and will be different

http://xxx.xxx.com/home/user/public_html/afe1f78ef6233580f2739a69be00027f.jpg

thank you before


Solution

  • I think you can modify the location of your assets by either modifying your app.php file or I think even better adding a variable to your .env to set the location of your assets depending if they differ in local and production.

    app.php

    'asset_url' => env('ASSET_URL', null),
    

    or

    .env

    ASSET_URL=path/to/folder