从编辑表格Laravel更新图像

时间:2017-10-19 11:34:35

标签: php laravel intervention

我想在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('/');
}

它出了什么问题?

2 个答案:

答案 0 :(得分:1)

当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('/');
}

答案 1 :(得分:0)

请使用以下代码:

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('/');
}