从编辑表单更新图像 Laravel

Update image from edit form Laravel

我想编辑 Laravel 中的博客表单。 Title, Body 等所有其他文本信息均已成功编辑。但是 Image 无法更新。新图片未上传,图片路径设置为C:\xampp\tmp\php2030.tmp.

我的控制器进行编辑。

public function update(Request $request, $id)
{
    $requestData = $request->all();

    $post = Post::findOrFail($id);
    $post->update($requestData);
    if ($request->hasFile('image'))
    {
        $file = $request->file('image');
        $fileNameExt = $request->file('image')->getClientOriginalName();
        $fileNameForm = str_replace(' ', '_', $fileNameExt);
        $fileName = pathinfo($fileNameForm, PATHINFO_FILENAME);
        $fileExt = $request->file('image')->getClientOriginalExtension();
        $fileNameToStore = $fileName.'_'.time().'.'.$fileExt;
        $pathToStore = public_path('media');
        Image::make($file)->resize(600, 531)->save($pathToStore . DIRECTORY_SEPARATOR. $fileNameToStore);
        $image = '/images/'.$fileNameToStore;   
        $post->save();                  
        }   
    session()->flash('message', 'Successfully updated the post');
    return redirect('/');
}

有什么问题吗?

当 PHP 收到文件上传时,默认情况下它会将其写入临时目录,就像您正在获取的那样,并在处理请求后自动删除文件。

您需要做的是将上传的文件移动到安全的位置。 Laravel 5.5 有一个 store method 文件上传功能,您可能会感兴趣。

public function update(Request $request, $id)
{
    $requestData = $request->all();
    $post = Post::findOrFail($id);

    if ($request->hasFile('image')) {
        $file = $request->file('image');
        $fileNameExt = $request->file('image')->getClientOriginalName();
        $fileNameForm = str_replace(' ', '_', $fileNameExt);
        $fileName = pathinfo($fileNameForm, PATHINFO_FILENAME);
        $fileExt = $request->file('image')->getClientOriginalExtension();
        $fileNameToStore = $fileName.'_'.time().'.'.$fileExt;
        $pathToStore = public_path('media');
        Image::make($file)->resize(600, 531)->save($pathToStore . DIRECTORY_SEPARATOR. $fileNameToStore);

        // UPDATE TEMPORARY IMAGE PATH WITH ACTUAL PATH
        $requestData['image'] = "/media/{$fileNameToStore}";
    }

    $post->update($requestData);
    session()->flash('message', 'Successfully updated the post');
    return redirect('/');
}

请使用以下代码:

public function update(Request $request, $id)
{
    $requestData = $request->all();
    $post = Post::findOrFail($id);

    $pathToStore = public_path('media');

    if ($request->hasFile('image')) 
    {
        $file = $request->file('image');
        $rules = array('file' => 'required|mimes:png,gif,jpeg'); // 'required|mimes:png,gif,jpeg,txt,pdf,doc'
        $validator = \Illuminate\Support\Facades\Validator::make(array('file'=> $file), $rules);

        if($validator->passes()) 
        {
            $filename = $file->getClientOriginalName(); 
            $extension = $file -> getClientOriginalExtension();
            $picture = sha1($filename . time()) . '.' . $extension;
            $upload_success = $file->move($pathToStore, $picture);

            if($upload_success)
            {
                //if success, create thumb
                $image = Image::make(sprintf($pathToStore.'/%s', $picture))->resize(600, 531)->save($pathToStore.'/thumb/'.$picture);
            }
        }

        $requestData['image'] = "$pathToStore/{$picture}";

    }

    $post->update($requestData);
    session()->flash('message', 'Successfully updated the post');
    return redirect('/');
}