Can't write image data to path

2.1k views Asked by At

for some reason i can't save pictures with the intervention tool. I have done this before, and i follow the same thing here, but now it's not being saved. The error im getting is:

NotWritableException in Image.php line 143:
Can't write image data to path (static/photos/1481902023.jpg)

here is my store function in the controller:

         $this->validate($request, array(
         'title'=>'required|max:255',
         'slug'=>'required|min:3|max:255|unique:posts',
         'body'=>'required',
         'img'=>'image',
         'desc'=>'max:255'
        ));

    $post=new Post;
    $post->title=$request->title;
    $post->slug=$request->slug;
    $post->body=$request->body;
    $post->desc = $request->desc;
    if($request->hasFile('img')){
        $image=$request->file('img');
        $imageName=time().'.'.$image->getClientOriginalExtension();
        $location='static/photos/'.$imageName;
        Image::make($image)->save($location);
        $post->image=$imageName;
    }

    $post->save();
    $post->tags()->sync($request->tags, false);
    $post->categories()->sync($request->categories, false);
    /*Session::flash('success', 'The Post Has been published!');*/
    return redirect()->route('slug', $post->slug); 

this should work like this exactly, but when i saw it did not work for some reason, i tried also the and it's not working too.

public_path()

the folder itself(static) is in the root directory of the project.

the Form view for the upload is this:

{{csrf_field()}}

        <div class="{{$errors->has('img')?'has-error':''}}">
         <label class="label" for="img">Upload picture</label>
           <div>
        <input type="file" name="img" id="img" class="input" multiple="multiple">
                </div>
             </div>

In addition to all this, i have also checked the permission to that folder(and sub folders), and even ran the command

icacls "C:\Users\GabMic\b\static" /grant GabMic:F

What am i missing here? Ps, im using the latest version of laravel. Thanks for the help.

2

There are 2 answers

5
xhulio On

Try the below code. First declare your path and then verify if the folder exists or not.

if($request->hasFile('img')){
    $image=$request->file('img');
    $path = base_path("static/photos/");
    File::exists($path) or File::makeDirectory($path, 0777, true, true);
    $img = Image::make($image->getRealPath())->save($path . time().'.'.$image->getClientOriginalExtension();
}
2
Merabi Pkhaladze On

Try:

$location='../public/static/photos/'.$imageName;

or

$location='../storage/static/photos/'.$imageName;
        Image::make($image)->save($location);