dpv21589 2018-08-25 08:48
浏览 247

图像保存为tmp文件

I'm trying to save image using Laravel

Image is saved as tmp file in database, why so?

the image saved as C:\xampp\tmp\phpA3EB.tmp in the database

how can I fix this?

in the controller:

public function update(Request $request, Bank $bank)
{
    if ( isset($request->photo) && $request->photo ) {

        $request['image'] = UploadImage($request->file('photo'), 'bank', '/banks');

        @unlink(public_path('/uploads/banks/') . $bank->image);
    }

    $updated = $bank->update($request->all());

    $bank->updateTranslations([
        'name' => $request->get('name_en'),
    ]);

    return $updated ?
        redirect()->route('banks.index')->with('success', trans('messages.updateTrue')) :
        redirect()->back()->with('warning', trans('messages.updateFalse'));
}


function UploadImage($inputRequest, $prefix, $folderNam)
{
    $imageName = $prefix.'_'.time().'.'.$inputRequest->getClientOriginalExtension();

    $destinationPath = public_path('/uploads/'.$folderNam);

    $inputRequest->move($destinationPath, $imageName);

    return $imageName ? $imageName : false;
}
  • 写回答

2条回答 默认 最新

  • duanhuang7591 2018-08-25 08:55
    关注

    please try this :

    public function UploadImage($image, $path)
    {
        $type = $image->getMimeType();
        $ext = substr($type, 6, strlen($type) -1 );
    
        $picName = uniqid() . '.' .$ext;
    
        $image->move(public_path($path), $picName);
    
        return $path . '/' . $picName;
      }
    }
    
    评论

报告相同问题?