Uploading image to server does not work

Asked

Viewed 484 times

2

When I upload locally, using WAMP64, everything happens normally. However, when I upload by the site, which is on the production server, the following error message is displayed.

Nowritableexception in Image.php line 143:

Can’t write image data to path (/home/premiumcarstudio/Laravel/public/1481746280.jpeg)

Can't write image data to path

Code I’m trying to upload

if ($request->hasFile('fileUpload')) {
        $image = $request->file('fileUpload');
        $filename  = time() . '.' . $image->getClientOriginalExtension();
        $path = public_path('images/ImagensPosts/' . $filename);
        Image::make($image->getRealPath())->resize(570,350)->save(public_path($filename));
        $post->imagem = 'images/ImagensPosts/'.$filename;
    }
    
    $post->save();

OBS. I’ve already changed the permissions to access the image directory on the server.

Directory on the Server inserir a descrição da imagem aqui

1 answer

1

In the Laravel 5.3, the recommended default is to save in Storage/app folder, which already has write permission. I usually use:

$request->nome_do_campo_do_arquivo->storeAs('pasta_de_destino', 'nome_do_arquivo.ext')

This creates the destination folder (if it does not exist) and gives a new name to the file.

If you are using previous versions, try giving write permission in the public/ folder. I hope I helped.

To see the documentation, click here.

  • I managed to upload this image to the server through your command, but I could not display the image, could pass me like this your <img> tag... I tried to put the paths even localhost and the image is not found... thanks

  • Situation resolved with that response

Browser other questions tagged

You are not signed in. Login or sign up in order to post.