Problems uploading files in Laravel 5.5 project in a hosting

1

I am uploading a Laravel 5.5 project to the free 000webhost hosting. A user can upload images to a post, when I try it on the computer without uploading it to a hosting it works perfectly. The image is uploaded to the public / image folder of the project and correctly saves the address where it is saved in a field called file, which is a post field.

When I upload the project to the hosting with the file manager, I save the public folder of the project in public_html and the rest of the folders in a folder called uejil.

Uploading images instead of saving them in public_html / image is saved in uejil / public / image. In addition to this when you save the address, save it in this way by giving an example: link

The name of the image is correct but not the address. Due to this, the uploaded image of the post can not be displayed.

I add the filesystems code:

return [

/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/

'default' => env('FILESYSTEM_DRIVER', 'local'),

/*
|--------------------------------------------------------------------------
| Default Cloud Filesystem Disk
|--------------------------------------------------------------------------
|
| Many applications store files both locally and in the cloud. For this
| reason, you may specify a default "cloud" driver here. This driver
| will be bound as the Cloud disk implementation in the container.
|
*/

'cloud' => env('FILESYSTEM_CLOUD', 's3'),

/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "s3", "rackspace"
|
*/

'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

    'public' => [
        'driver' => 'local',
        'root' => public_path(),
        'url' => env('APP_URL').'/storage',
        'visibility' => 'public',
    ],

    's3' => [
        'driver' => 's3',
        'key' => env('AWS_ACCESS_KEY_ID'),
        'secret' => env('AWS_SECRET_ACCESS_KEY'),
        'region' => env('AWS_DEFAULT_REGION'),
        'bucket' => env('AWS_BUCKET'),
        'url' => env('AWS_URL'),
    ],

],

];

And the post driver code

<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;  
use App\Http\Requests\PostStoreRequest;
use App\Http\Requests\PostUpdateRequest;
use App\Http\Controllers\Controller;

use Illuminate\Support\Facades\Storage;


use App\Post;
use App\Category;
use App\Tag;

class PostController extends Controller
{

public function __construct()
{
    $this->middleware('auth');
}

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $posts = Post::orderBy('id', 'DESC')
    ->where('user_id', auth()->user()->id)
    ->paginate();


    return view('admin.posts.index', compact('posts'));
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    $categories = Category::orderBy('name', 'ASC')->pluck('name', 'id');
    $tags       = Tag::orderBy('name', 'ASC')->get();

     return view('admin.posts.create', compact('categories', 'tags'));
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(PostStoreRequest $request)
{
    $post = Post::create($request->all());
    $this->authorize('pass', $post);

    //IMAGE 
    if($request->file('file')){
        $path = Storage::disk('public')->put('image',  $request->file('file'));
        $post->fill(['file' => asset($path)])->save();
    }

    //NOTAS
    if($request->file('file2')){
        $path = Storage::disk('public')->put('notas',  $request->file('file2'));
        $post->fill(['file2' => asset($path)])->save();
    }

    //TAGS
    $post->tags()->attach($request->get('tags'));

    return redirect()->route('posts.edit', $post->id)->with('info', 'Entrada creada con éxito');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    $post = Post::find($id);
    $this->authorize('pass', $post);

    return view('admin.posts.show', compact('post'));
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    $post       = Post::find($id);
    $this->authorize('pass', $post);

    $categories = Category::orderBy('name', 'ASC')->pluck('name', 'id');
    $tags       = Tag::orderBy('name', 'ASC')->get();

    return view('admin.posts.edit', compact('post', 'categories', 'tags'));
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(PostUpdateRequest $request, $id)
{
    $post = Post::find($id);
    $this->authorize('pass', $post);

    $post->fill($request->all())->save();

    //IMAGE 
    if($request->file('file')){
        $path = Storage::disk('public')->put('image',  $request->file('file'));
        $post->fill(['file' => asset($path)])->save();
    }

    //NOTAS
    if($request->file('file2')){
        $path = Storage::disk('public')->put('notas',  $request->file('file2'));
        $post->fill(['file2' => asset($path)])->save();
    }

    //TAGS
    $post->tags()->sync($request->get('tags'));

    return redirect()->route('posts.edit', $post->id)->with('info', 'Entrada actualizada con éxito');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    $post = Post::find($id);
    $this->authorize('pass', $post);
    $post->delete();

    return back()->with('info', 'Eliminado correctamente');
}
}

If you need to add another code, please advise.

    
asked by Kinafune 12.04.2018 в 03:26
source

1 answer

3

You have to register the new public route since it is no longer public but public_html

\App\Providers\AppServiceProvider

    /**
 * Register any application services.
 *
 * @return void
 */
public function register()
{

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

link

    
answered by 12.04.2018 / 09:55
source