Neeraj1005's avatar

Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException The file "C:\xampp\tmp\php1AC.tmp" does not exist

Here i have put my controller method. I did not understand why this error comes when I upload an image. Problem is data is stored but I don't understand what is the cause for this error?

    public function store(PostRequest $request, Post $post)
    {
        // dd($request->all());

        $input = $request->validated();

        if($file = $request->file('photo_id')){


            $name = time() . $file->getClientOriginalName();


            $file->move('images', $name);

            $photo = Photo::create(['file'=>$name]);


            $input['photo_id'] = $photo->id;

        }

        $post = auth()->user()->posts()->create($input);

        // dd($post);
        $post->tags()->attach(request('tags'));

        return redirect(route('posts.index'))->with('message','Posts created succesfully');
    }
0 likes
12 replies
Neeraj1005's avatar

@michaloravec laravel 7.9.2 or you can help me? how can i return an error as an exception. In above I have store method. How can I apply try catch method for this? plz tell?

MichalOravec's avatar

In your php.ini file change the following

upload_max_filesize = 30M
post_max_size = 30M

And restart your local server

MichalOravec's avatar
$name = time() . $file->getClientOriginalName();

$path = $request->photo_id->storeAs('images', 'filename.jpg');

$photo = Photo::create(['file'=> $path]);

$input['photo_id'] = $photo->id;

And read documentation, which I posted before.

Neeraj1005's avatar

@michaloravec I made a little bit modification in my store function now this error comes.

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'C:\xampp\tmp\phpA94C.tmp' for column `codehacking`.`posts`.`photo_id` at row 1 (SQL: insert into `posts` (`title`, `body`, `photo_id`, `category_id`, `user_id`, `updated_at`, `created_at`) values (image, asdf, C:\xampp\tmp\phpA94C.tmp, 1, 1, 2020-05-07 14:22:15, 2020-05-07 14:22:15))

plz look at the code

    public function store(PostRequest $request, Post $post)
    {

        // dd($request->validated());

        $input = $request->validated();

        if($request->hasFile('file')){

            $originalname = $request->file->getClientOriginalName();

            $extension = $request->file->extension();

            $uploadpath = $request->file->storeAs('media', $originalname, 'public');

            $photo = Photo::create(['file'=> $uploadpath]);

            $input['photo_id'] = $photo->id;

        }

        $post = auth()->user()->posts()->create($input);

        $post->tags()->attach(request('tags'));//for store the tags

        return redirect(route('posts.index'))->with('message','Posts created succesfully');
    }
MichalOravec's avatar

In your form change photo_id to for example path. And you have propably Photo model, so associate it with your post.

Neeraj1005's avatar

@michaloravec this is my blade file upload form

     <div class="form-group">
<label for="photo_id">PostImage</label>
<input id="photo_id" class="form-control-file @error('photo_id') is-invalid @enderror" type="file" name="photo_id" aria-describedby="fileHelp">
<small id="fileHelp" class="form-text text-muted">
 Please upload a valid image file. Size of image should not be more than 2MB.
 </small>
                                    @error('photo_id')
<small class="form-text text-red">{{ $message }}</small>
                                    @enderror
    </div>
MichalOravec's avatar
Level 75

Change it like this, then of course you have to change your controller as well.

<div class="form-group">
    <label for="path">PostImage</label>

    <input id="path" class="form-control-file {{ $errors->first('path', 'is-invalid') }}" type="file" name="path" aria-describedby="fileHelp">

    <small id="fileHelp" class="form-text text-muted">
        Please upload a valid image file. Size of image should not be more than 2MB.
    </small>

    @error('path')
        <small class="form-text text-red">{{ $message }}</small>
    @enderror
</div>
1 like
Neeraj1005's avatar

@michaloravec like this? in controller

       $input = $request->validated();

        if($request->hasFile('path')){

            $originalname = $request->path->getClientOriginalName();

            $extension = $request->path->extension();

            $uploadpath = $request->path->storeAs('media', $originalname, 'public');

            $photo = Photo::create(['file'=> $uploadpath]);

            $input['photo_id'] = $photo->id;

        }
MichalOravec's avatar

@neeraj1005 And I hope that you have photo relationship on post model.

public function store(PostRequest $request, Post $post)
{
    $input = $request->validated();

    $post = auth()->user()->posts()->create($input);

    $post->tags()->attach(request('tags'));

    if ($request->hasFile('path')) {
        $uploadpath = $request->path->store('media', 'public');

        $photo = Photo::create(['file'=> $uploadpath]);

        $post->photo()->associate($photo);

        $post->save();
    }

    return redirect(route('posts.index'))->with('message','Posts created succesfully');
}

Please or to participate in this conversation.