“ C:\ xampp \ tmp \ php6A75.tmp”文件不存在或不可读

时间:2019-10-31 13:21:57

标签: laravel file-upload laravel-6.2

我想在laravel-6.2中上传图片,但显示错误:

  

“ C:\ xampp \ tmp \ php99C0.tmp”文件不存在或不可读

我的管理员控制器:

class AdminController extends Controller
{
    protected function uploadImages($file){

        $year = Carbon::now()->year;
        $imagePath = "/upload/images/{$year}/";

        $filename= $file->getClientOriginalName();

        $file = $file->move(public_path($imagePath) , $filename);

        $sizes=['300' , '600' , '900'];
        // resize the image to a width of 300 and constrain aspect ratio (auto height)
        $url['images'] = $this->resize($file->getRealPath() , $sizes , $imagePath , $filename);
        $url['thumb'] = $url['images'][$sizes[0]];

        return $url;
    }


    private function resize($path , $sizes , $imagePath , $filename){

        $images['original'] = $imagePath . $filename ;

        foreach ($sizes as $size){
            $images[$size] = $imagePath ."{$size}_" . $filename ;

            Image::make($path)->resize($size, null, function ($constraint) {
                $constraint->aspectRatio();
            })->save(public_path($images[$size]));

        }

        return $images;
    }

}

和我的CourseController存储功能代码:

 public function store(CourseRequest $request)
    {


        $imageUrl = $this->uploadImages($request->file('images'));

        auth()->user()->course()->create(array_merge($request->all() , ['images' => $imageUrl]));


        return redirect(route('courses.index'));
    }

我的表单查看代码:


 <form action="{{route('courses.store')}}" method="post" enctype="multipart/form-data">
                        {{csrf_field()}}
                        @include('Admin.section.errors')
                        <div class="form-group">
                            <label for="title" class="bmd-label-floating text-white">Course Title</label>
                            <input type="text" class="form-control" name="title" id="title" placeholder="Enter Course title" value="{{old('title')}}">
                        </div>

                        <div class="form-group col-md-4">
                            <label for="Type">Course Type</label>
                            <select id="Type" name="type" class="form-control">
                                <option value="vip" class="text-dark">VIP</option>
                                <option value="cash" class="text-dark">Cash</option>
                                <option value="free" class="text-dark" selected>free</option>
                            </select>
                        </div>

                        <div class="form-group mt-3">
                            <label for="body" class="bmd-label-floating text-white">Course Body</label>
                            <textarea rows="15" class="form-control" name="body" id="body" placeholder="Enter Course body">{{old('body')}}</textarea>
                        </div>

                            <div class="form-file-upload mt-4">
                                <label for="pictures" class=" text-white">Course Pictures</label>
                                <input type="file" name="images" class="form-file-upload" id="pictures"  placeholder="Enter pictures" value="{{old('images')}}">
                            </div>

                        <div class="form-group mt-5">
                            <label for="tags" class=" text-white">Course Price</label>
                            <input type="text" name="price" class="form-control" id="tags"  placeholder="Enter Price" value="{{old('price')}}">
                        </div>
                            <div class="form-group mt-1">
                                <label for="tags" class=" text-white">Course Tags</label>
                                <input type="text" name="tags" class="form-control" id="tags"  placeholder="Enter Tags" value="{{old('tags')}}">
                            </div>



                        <button type="submit" class="btn btn-primary">Submit</button>
                    </form>

请帮助我。

1 个答案:

答案 0 :(得分:0)

如果使用Storage类,则可以将映像移到存储文件夹中的正确路径。

Storage::disk('local')->put('/uploads/images/' . $year .'/', $filename);

您需要运行命令PHP Artisan storage:link在存储文件夹和公用文件夹之间创建符号链接。