0

When I upload images to a storage folder in my localhost, it works fine. However, when I move my Laravel 5.7 project to shared hosting the images don't appear, and I don't know why.

Thank you for your help.

Blade

@foreach ($data as $user)
    @foreach(json_decode($user->name, true) as $images)
    @endforeach
@endforeach
<!-- Wrapper for slides -->
<div class="carousel-inner">
    @foreach ($data as $user)
        @foreach(json_decode($user->name, true) as $images)
            <div class="item">
                <img src="{{ asset('/slider/images') }}/{{$images}}" alt="Los Angeles" style="width:100%;">
            </div>
        @endforeach
    @endforeach
</div>

Controller

<?php

if ($request->hasfile('image')) {
    foreach ($request->file('image') as $image) {
        $name = $image->getClientOriginalName();
        $image->move(storage_path() . '/slider/images/', $name);
        $data[] = $name;
    }
    $query = DB::table('slider')->insert([
        ['title' => 'lorem', 'alt' => 'lorem', 'name' => json_encode($data)],
    ]);

    return "good";
} else {
    return "null";
}
Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Abdo Abo
  • 93
  • 1
  • 2
  • 13

3 Answers3

0

Please try login using ssh and run

php artisan storage:link

or create .htaccess to your root laravel folder and add this code below.

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

and make sure you have proper permission to your storage folder.

that could probably helps.

Jesus Erwin Suarez
  • 1,571
  • 16
  • 17
  • I run this command in my localhost all it's good but in my shared hosting doesn't show images – Abdo Abo Dec 01 '18 at 15:39
  • can you review the access.log/error.log of your server? what does it say against attempts to load an image from your project? – joeyj Dec 01 '18 at 15:59
  • also "doesn't show images" is vague. Please be more specific. Is it a white page? An nginx error? a web error code i.e 404? – joeyj Dec 01 '18 at 16:01
0

https://stackoverflow.com/a/50771337/3240068

Your shared hosting server may or may not allow symlinks. If you run the command and it still does not work, then you have two options, basically.

1) Upload / move / copy images from storage to public;

2) https://laravel.com/docs/5.7/responses#file-downloads

Create a route and controller that reads and returns images from storage. This is not tested, so do some proof-reading and research, but it should go something like this:

// Route
Route::get('storage-asset/{$path}', 'App\Http\SomeController@getStorageAsset');

// Controller
public function getStorageAsset($path)
{
    if (File::exists(storage_path($path)) {
        return response()->file(storage_path($path));
    }

    // If file not found.
    return abort(404)
}
Criss
  • 952
  • 5
  • 17
0

You need to create a file (e.g, link.php) inside the public folder and declare the codes below. make sure to declare your path as is in your app.

<?php
    symlink('../public_html/storage/app/public', '../public_html/public/storage');

then, you can visit your-domain.com/link.php, and that's it. done

Chetam Okafor
  • 493
  • 4
  • 7