簡體   English   中英

RuntimeException SplFileInfo::getSize(): stat failed for... Laravel 6 上傳圖片

[英]RuntimeException SplFileInfo::getSize(): stat failed for… Laravel 6 upload image

我使用 laravel 6(最后更新),我創建了一個可以上傳圖像的表單。 我在 Windows 操作系統上,我使用 PHPStorm、laravel 和 xampp 應用程序。 我所有的配置都設置正確,沒有運行問題。

我的問題是,當我從表單提交到達字段時出現此錯誤:

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

這是我的表單 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

這是我的 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
        ]);

上傳的文件代碼完美運行,我在我想要的選定文件夾中注冊文件。 我的路線沒有問題(無需顯示這部分代碼)。 並且數據庫沒有計算來自創建帖子的記錄。

當我提交表單時,我有這個錯誤:

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

我已經檢查了laravel/vendorphp.iniUploadedFile.php中的upload_max_filesize ,它們具有相同的值。

如果您有任何想法...謝謝。

我正在解決同樣的問題,請有人幫助我

我知道現在回答已經晚了,但為了別人的知識。 我對 laravel 6 有同樣的問題。

C 的統計失敗:\xampp\tmp\phpA5C6.tmp\

這個錯誤指出了確切的原因,這是因為請求獲取了擴展名為.tmp的臨時上傳文件,並嘗試將此值分配給數據庫中的字段。 在您的驗證中,您強制它僅獲取具有已知擴展名的圖像。 所以我做了這些步驟:

  • 在代碼中使用相同的數據庫字段名稱(圖像)在刀片文件中創建隱藏輸入。

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

    並將文件輸入字段重命名為與數據庫列名稱無關的其他名稱(比如 some_pic)

  • 在您的 Controller 中,驗證后,上傳您的文件,然后准備插入數據庫

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

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

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

    那么您將在插入請求時排除文件輸入

    $post=Post::create($request->except('some_pic'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM