未定义的变量:插入

我打算将“annonces\ August2020 \ image.jpg”之类的图像链接插入到数据库和存储中的图像中,但它给了我错误未定义的变量:插入。


文件控制器.php


public function save(Request $request)

    {

       request()->validate(['file'  => 'required|mimes:doc,docx,pdf,txt,jpeg,jpg|max:2048',]);


       if ($files = $request->file('fileUpload')) {

           $destinationPath = 'public/file/'; // upload path

           $profilefile = date('YmdHis') . "." . $files->getClientOriginalExtension();

           $files->move($destinationPath, $profilefile);

           $insert['file'] = "$profilefile";

        }

        $check = Document::insertGetId($insert);

        return Redirect::to("file")

        ->withSuccess('Great! file has been successfully uploaded.');

    }

文件.balde.php


<form action="/save" method="post" enctype="multipart/form-data">

                @csrf

                <div class="form-group">

                    <input type="file" class="form-control-file" name="file" id="file" 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>

                </div>

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

            </form>

网页.php


Route::get('file', 'fileController@index');

Route::post('save', 'fileController@save');


心有法竹
浏览 92回答 3
3回答

哈士奇WWW

首先,如果你想存储图像,那么你应该使用laravel 的存储功能来存储它。让我只向您展示上传图像并将其保存在数据库中的代码。if($request->hasFile('file')){&nbsp; &nbsp; &nbsp; &nbsp; $file = $request->file('file');&nbsp; &nbsp; &nbsp; &nbsp; $fileName = time().$file->getClientOriginalName();&nbsp; &nbsp; &nbsp; &nbsp; Storage::put('public/file'.$fileName,file_get_contents($file));&nbsp; &nbsp; &nbsp; &nbsp; //file is uploaded and now you can store the name of the file in database&nbsp;&nbsp; &nbsp; }请添加到use Storage控制器文件的顶部。现在您可以通过以下方式访问它:<img src="{{asset('storage/file'.$namefromdatabase)}}" >php artisan storage:link在上传存储目录中的图片之前不要忘记运行此命令

MMTTMM

public function save(Request $request)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$insert=[];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;request()->validate(['file'&nbsp; => 'required|mimes:doc,docx,pdf,txt,jpeg,jpg|max:2048',]);&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ($files = $request->file('fileUpload')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$destinationPath = 'public/file/'; // upload path&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$profilefile = date('YmdHis') . "." . $files->getClientOriginalExtension();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$files->move($destinationPath, $profilefile);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$insert['file'] = "$profilefile";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $check = Document::insertGetId($insert);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return Redirect::to("file")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->withSuccess('Great! file has been successfully uploaded.');&nbsp; &nbsp; &nbsp; &nbsp; }这段代码中定义了$insert,请尝试一下。

慕的地10843

看这一行:$insert['file']&nbsp;=&nbsp;"$profilefile";这表示,在可能关联的数组中,名为 的$insert键的元素从现在开始'file'将具有值。"$profilefile"您的代码崩溃了,因为$insert未初始化。显然 PHP 的解释器没有你聪明,不知道这$insert应该是一个关联数组。你需要有一条类似的线$insert&nbsp;=&nbsp;[];这会将 insert 定义为数组。请注意,必须在将值赋给 的元素之前执行此行$insert。
打开App,查看更多内容
随时随地看视频慕课网APP