1

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

  • btw, I've also attempt to modify the path for controller, but it doesn't help either :( – user12297722 Nov 11 '19 at 07:07
  • hmmm... I am not sure I get the whole picture of your question. But I think one things you seem to be using wrong is your asset helper. `src="{{ asset($news->news_photo_url) }}" ` should be something like `src="{{ asset('/img/news/' . $news->news_photo_url) }}"` based on your controller. – Eli Nov 11 '19 at 07:58
  • apologize, it is quite hard to actually explain it myself, I mean, does this happen all the time? this might be out of topic but laravel app folder in shared hosting should not be located in public_html right? – user12297722 Nov 11 '19 at 08:13
  • I want to avoid modify that asset helper .. I tried modify this too ```$app->bind('path.public', function() {return __DIR__; });``` to ```$app->bind('path.public', function() {return __DIR__.'img/news'; });``` but instead of moving the files to that folder, it create another folder name "img/news" – user12297722 Nov 11 '19 at 08:14
  • well I think the most common place to put your public folder is as intended in the framework which is in the root of your proyect, but you should still be able to place it somewhere else, I would suggest you look into configuring your asset url, you can look at this configuration in the `app.php` file or add it in you .env with the name `ASSET_URL` – Eli Nov 11 '19 at 08:30
  • I see, thank you for your help, I will try as you suggest, do you mind putting your comment to the answer, I will accept your answer – user12297722 Nov 12 '19 at 03:26
  • there it is, hope it helps, let me know if you where able to solve your problem :) – Eli Nov 12 '19 at 12:57

1 Answers1

0

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
Eli
  • 964
  • 9
  • 21