Image upload problem Laravel/Intervention

Asked

Viewed 790 times

1

I have a problem when I try to upload an image, well, when I give Submit in the form it returns the following error:

Intervention\Image\Exception\NotWritableException
Can't write image data to path (public/images/products/2014-06-01-16:24:38-581165_571926502843010_889453779_n.jpg)

Well, I figured it might be permission issue, so I went over there and gave permission 777 in the directory, but the bastard keeps making the same mistake. Just follow my code:

public function postCreate(){
            $validator = Validator::make(Input::all(),Product::$rules);

            if($validator->passes()){
                    $product = new Product();
                    $product->category_id   = Input::get('category_id');
                    $product->title                 = Input::get('title');
                    $product->description   = Input::get('description');
                    $product->price                 = Input::get('price');
                    $product->availability  = Input::get('availability');

                    $image = Input::file('image');
                    $filename = date('Y-m-d-H:i:s').'-'.$image->getClientOriginalName();
                    Image::make($image->getRealPath())->resize('468,249')->save('public/images/products/'.$filename);
                    $product->image                 = 'images/products/'.$filename;
                    $product->save();

                    return Redirect::to('admin/products/index')
                            ->with('message','Produto cadastrado');
            }
}
  • Can’t you provide a more verbose error message? It’s just that this error message is too wide. It can give room for a lot of things.

3 answers

1

I am grateful for all the answers, but I have solved them as follows:

$filename = date('Y-m-d-H-i-s').'.'.Input::file('image')->getClientOriginalExtension();
Image::make(Input::file('image')->getRealPath())->resize('468,249')->save( public_path('img/products/'.$filename));

I provided the absolute path of the file and not the Relatio, from the root of the project. With this he uploaded, the solution was given in a question I asked in the Laravel Brasil group.

Question in the Laravel Brasil group

Thank you so much for all your help!!!

0

I’m not familiar with Laravel, but I suggest a change in the file name.

I don’t know which O.R. is using, but I suggest removing the character ":" of your file name, since in Windows and Linux it is an invalid character. See documentation from Windows and a recommendation in this website. Change ":" for "_" or any other valid character and see the result.

Adapting the following line:

$filename = date('Y-m-d-H_i_s').'-'.$image->getClientOriginalName();
  • So I made that change but the mistake persists. If I happen to put as a md5 hash, it gives the same error tbm: $filename = md5(date('Y-m-d-H_i_s')). '-'. $image->getClientOriginalName();

0

Try it like this:

public function postCreate(){
    $validator = Validator::make(Input::all(),Product::$rules);

    if($validator->passes()){
            $product = new Product();
            $product->category_id   = Input::get('category_id');
            $product->title         = Input::get('title');
            $product->description   = Input::get('description');
            $product->price         = Input::get('price');
            $product->availability  = Input::get('availability');

            $image = Input::file('image');
            $filename = date('Y-m-d-H:i:s').'-'.$image->getClientOriginalName();
            Image::make($image->getRealPath())->resize('468,249')->save('images/products/'.$filename);
            $product->image         = 'images/products/'.$filename;
            $product->save();

            return Redirect::to('admin/products/index')
                    ->with('message','Produto cadastrado');
    }
}

Why put public in the save and does not need to, so it is not valid path. By default do not need to put the public that the framework already recognizes such a path

Browser other questions tagged

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