集成环境:xampp 64位 3.3.2
php版本:7.2.14
public function editProduct()
{
if (request()->isAjax()) {
$data = input();
// $f = $_FILES;
// var_dump($f['pimg']['tmp_name']);
// die;
// if ($f['pimg']['tmp_name']) {
$id = input('?pid') ? input('pid/d') : '';
$editData = Db::table('product')->where('pid', $id)->find();
$upload = new Upload();
$file = request()->file('pimg');
if ($file) {
// 更改图片信息
$folder = 'products';
$image_path = $upload->upload($file, $folder);
// 更新的图片文件插入到upload文件夹中
if ($image_path) {
// 找到旧文件,删除旧文件
if ($editData['pimg']) {
if (file_exists('../public' . $editData['pimg'])) {
unlink('../public' . $editData['pimg']);
// 删除文件
}
}
// 重新写入数据库
$data['pimg'] = $image_path;
} else {
// 图片没有插入到文件夹中
$res['msg'] = '上传图片到文件夹中操作失败';
}
} else {
// 未更改图片信息
if ($editData['pimg']) {
$data['pimg'] = $editData['pimg'];
}
// else {
// $res['msg'] = '没有图片数据';
// }
}
// }
$info = Db::table('product')->update($data);
if ($info) {
$this->success('数据更新完成', 'index');
} else {
$this->error('数据更新失败');
}
} else {
// 读取被修改的原始数据
$id = input('?id') ? input('id/d') : '';
// dump($id);
// die;
$editData = Db::table('product')
->where('pid', $id)
->find();
$this->assign('product', $editData);
return view('editProduct');
}
}出现下列错误[0] Exception in Request.php line 1261
没有文件被上传!
2 => 'upload File size exceeds the maximum value',
3 => 'only the portion of file is uploaded',
4 => 'no file to uploaded',
6 => 'upload temp dir not found',
7 => 'file write error',
];
$msg = $fileUploadErrors[$error];
throw new Exception($msg);
}
/**
* 获取环境变量
* @access public
* @param string $name 数据名称
* @param string $default 默认值
* @return mixed
*/
Call Stack
in Request.php line 1261
at Request->throwUploadFileError(4) in Request.php line 1235
at Request->dealUploadFile(['pimg' => ['name' => '', 'type' => '', 'tmp_name' => '', ...]], 'pimg') in Request.php line 1187
at Request->file('pimg') in Product.php line 72
at Product->editProduct()
at ReflectionMethod->invokeArgs(ob
at Container->invokeReflectMethod(ob
at Module->think\route\dispatch\{closure}(ob
at call_user_func_array(ob
at Middleware->think\{closure}(ob
at call_user_func(ob
at Middleware->dispatch(ob
at Module->exec() in Dispatch.php line 168
at Dispatch->run() in App.php line 432
at App->think\{closure}(ob
at call_user_func_array(ob
at Middleware->think\{closure}(ob
at call_user_func(ob
at Middleware->dispatch(ob
at App->run() in index.php line 21
最佳答案