phplaravellaravel-5.4laravel-5.5bluehost

I can not upload images to the public path in Shared Server Laravel 5.5


I have a project hosted on a shared server in Blue Host and in the store and update method of the AdminPostController I have a function to save the images in the public_path, but it generates the following error when trying to save it:

Intervention\Image\Exception\NotWritableException
Can't write image data to path (/home4/directoryname/public/directoryname/uploads/posts/1516313973.jpg)

This is my store method:

public function store(Request $request)
{
    $posts = new Post();
    $posts->title           = $request->input('title');
    $posts->slug            = str_slug($request->get('title'));
    $posts->content         = $request->input('content');
    $posts->category_id     = $request->input('category_id');
    $posts->user_id         = $request->input('user_id');
    $posts->blockquote      = $request->input('blockquote');
    $posts->blockquote_sign = $request->input('blockquote_sign');
    $posts->save();

    // Handle the client upload image id avatar
    if($request->hasFile('avatar')){
        $avatar = $request->file('avatar');
        $filename = time() . '.' . $avatar->getClientOriginalExtension();
        Image::make($avatar)->resize(500, 300)->save( public_path('uploads/posts/' . $filename ) );

        $posts->avatar = $filename;
        $posts->save();
    }

    return redirect()->route('contenido.index')
                    ->with('success','Publicación creada correctamente!!!');
}

This is my update method:

public function update(Request $request, $id)
{
    $updated = Post::findorFail($id);

    $post = $request->all();

    $updated->fill($post)->save();

    if($request->hasFile('avatar')){
        $avatar = $request->file('avatar');
        $filename = time() . '.' . $avatar->getClientOriginalExtension();
        Image::make($avatar)->resize(500, 300)->save( public_path('uploads/posts/' . $filename ) );

        $updated->avatar = $filename;
        $updated->save();
    }

    return redirect()->route('contenido.index')
                    ->with('success','Publicación actualizada correctamente!!!');
}

This is my route resource for these methods:

Route::resource('contenido', 'Admin\AdminPostController');

I was reading different places and at the moment I have not found a solution, can someone guide me?.

This is how the files on the server are structured.

enter image description here


Solution

  • Laravel 5 by default uses the public folder as the home directory while my server uses another. Therefore, I set a alternative home directory.

    I just had to perform the following actions:

    1 - Edit index.php located in the public folder of your Laravel 5 project.

    2 - Find the following line:

    $app = require_once __DIR__.'/../bootstrap/app.php';
    

    3 - Below the that line add the following lines:

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

    4 - Now re-name the public directory of your project to public_html.

    The above edit to your index.php file will direct your Laravel project to use the immediate parent directory of this file as the public directory for your project.

    You can find more in this publication: https://laracasts.com/discuss/channels/general-discussion/where-do-you-set-public-directory-laravel-5