0

I work with laravel 6 (last update), I create a form where we could upload an image. I am on Windows OS, I use PHPStorm, laravel and xampp Applications. All my configuration is setting right, no running problems.

My probleme is that I have this error when I submit the reaching fields from my form:

RuntimeException SplFileInfo::getSize(): stat failed for C:\xampp\tmp\phpA5C6.tmp \

Here's the code from my form create.blade.php:

@extends('layouts.app')

@section('content')
  <div class="container">


    <div class="row justify-content-center">
      <div class="col-md-8">
        <div class="card">
          <div class="card-header">Creat Post</div>
          <div class="card-body">
            @if(count($errors) > 0)
              <ul>
                @foreach($errors->all() as $error)
                  <li class="alert alert-danger alert-dismissible fade show">
                    <strong>{{$error}}</strong>
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </li>
                @endforeach
              </ul>
            @endif
            <form action="{{route('post.store')}}" method="POST" enctype="multipart/form-data" >
              @csrf
              <div class="form-group">
                <label for="title">Title</label>
                <input type="text" name="title" class="form-control" id="title" aria-describedby="title" placeholder="Type Your Title">
              </div>
              <div class="form-group">
                <label for="category">Category</label>
                  <select class="form-control" name="category_id" id="category">
                    <option value="0" selected disabled>Select A Category</option>
                    @foreach($categories as $category)
                      <option value="{{$category->id}}">{{$category->name}}</option>
                    @endforeach
                  </select>
              </div>
              <div class="form-group">
                <label for="content">Content</label>
                <textarea class="form-control" name="content" id="content" rows="4" cols="4" placeholder="Type Your Content"></textarea>
              </div>
              <div class="form-group">
                <label for="image">Image</label>
                <input type="file" name="image" id="image" class="form-control-file">
              </div>
              <button type="submit" class="btn btn-primary">Save</button>
            </form>

          </div>
        </div>
      </div>
    </div>
  </div>
@endsection

Here's the code from my controller:

    public function store(Request $request)
    {
        $data = $this->validate($request, [
                "title"       => "required|string",
                "content"     => "required|string",
                "category_id" => "required",
                "image"       => "required|image"
              ]);

        $file = $request->image;
        $destinationPath = public_path().'/uploads/posts';
        $filename = $destinationPath . '' . time() . '.' . $file->getClientOriginalExtension();
        $uploaded = $file->move($destinationPath,$filename);


        $post = Post::create([
          "title"       => $request->title,
          "content"     => $request->content,
          "category_id" => $request->category_id,
          "image"       => $uploaded
        ]);

The uploaded file code works perfect, I register the file in the selected folder I want. I Have no problem with my routes (no need to show this part of the code). and the database didn't count the record from create post.

and When I submit the form, I have this error:

RuntimeException SplFileInfo::getSize(): stat failed for C:\xampp\tmp\phpA5C6.tmp

and I've checked the upload_max_filesize in the php.ini and the UploadedFile.php in laravel/vendor and they have the same value.

If you have any ideas... Thank you.

Salman Zafar
  • 3,844
  • 5
  • 20
  • 43
Ahmed Malek
  • 186
  • 1
  • 1
  • 8

1 Answers1

0

I Know it is late to answer, but for other's knowledge. I've the same issue with laravel 6.

stat failed for C:\xampp\tmp\phpA5C6.tmp \

This error points to the exact reason, It's because the request take the temporary uploaded file with extension .tmp, and try to assign this value to your field in database. In your validation you force it to get only image with known extensions. so I did these steps:

  • create hidden input in blade file with the same database field name(image) in your code.

    
    <input type="hidden" id="image" name="image">
    

    and rename the file input field to some other name not related to db columns names( say some_pic)

  • in your Controller, after validation, upload your file then prepare for inserting to the database

     $imageName = time().'.'.$request->some_pic->extension();
    
    

    $request->some_pic->move(public_path('images'), $imageName);

    if ($request->has('some_pic')) { $request->merge(['image' => $imageName]); }

    then you will except the file input when insert your request

    $post=Post::create($request->except('some_pic'));
Ahmed khaled
  • 51
  • 1
  • 7