kazyka's avatar

Getting error when uploading a file, but it's uploaded to directory

Hi, I try to upload a picture. I can't see what I am doing wrong, since the picture gets uploaded to the public_dir('img'), the public/img directory, but I get an error that says The image failed to upload. and the post is of course not created in my database. In my create.blade I have

{!! Form::model($post, [
    'method'=> 'POST',
     'route' => 'blog.store',
     'files' => TRUE
     ]) !!}

{!! Form::file('image') !!}

and in the controller I have

protected $uploadPath;

public function __construct()
{
    parent::__construct();
    $this->uploadPath = public_path('img');
}
.
.
.
public function store(Request $request)
{
    $data = $this->handleRequest($request);

    $this->validate($request, [
        'title'        => 'required',
        'body'         => 'required',
        'published_at' => 'date_format:Y-m-d H:i:s',
        'category_id'  => 'required',
        'image'        => 'mimes:jpg,jpeg,bmp,png',
        ]);


    $request->user()->posts()->create($data);

    return redirect('admin')->with('message', 'Your post was created');
}

public function handleRequest($request)
{
    $data = $request->all();

    if ($request->hasFile('image'))
    {
        $image         = $request->file('image');

        $fileName      = $image->getClientOriginalName();
        $destination   = $this->uploadPath;

        $image->move($destination, $fileName);

        $data['image'] = $fileName;
    }

    return $data;
}
0 likes
7 replies
king's avatar

This happens to me when the file is exceeding upload_max_filesize or post_max_size. Hope this helps.

kazyka's avatar

@king

The file I am trying to upload is 640x520 and is only 82.8 KB

So I don't think that is the problem :/

king's avatar

I'm guessing here but maybe its an issue with trying to upload a file with the same name.

kazyka's avatar

@king

I have tried different files and sizes, so doesn't seem so.

But the funny thing is that the file gets to the public folder, but the post can't get created and wont show up in the database.

But I have no problem creating posts if I do not try to upload a file

kazyka's avatar

@king

I moved my file in handleRequest() before I validated, just had to move handleRequest() below validate

king's avatar

I remember an instance where I couldn't the file upload because it was corrupted. If you don't validate does everything work as expected? Also does this happen with every file or just that one? Ok looks like you solved it yourself :)

kazyka's avatar

I still validate, but I just validate, before I do the handleRequest().

With the old code, I couldn't upload a single file

So it seems to work, with every file now, after I made the change

The changes I made can be seen below:

$this->validate($request, [
            'title'        => 'required',
            'body'         => 'required',
            'published_at' => 'date_format:Y-m-d H:i:s',
            'category_id'  => 'required',
            'image'        => 'mimes:jpg,jpeg,bmp,png',
            ]);
        
        $data = $this->handleRequest($request);
        
        $request->user()->posts()->create($data);

        return redirect('admin')->with('message', 'Your post was created');
1 like

Please or to participate in this conversation.