首先需要 use think\Request;
//上传文件
public function uploadfile(){
$data = input('post.');
//获取表单上传文件
$file = request()->file('files');
// dump($file);die;
if (empty($file)) {
$this->error('请选择上传文件');
}
//移动到框架应用根目录/public/uploads/ 目录下
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
// dump($info);die;
$data['pathName'] = $info->getpathName();
$data['getExtension'] = $info->getExtension();
$data['getSaveName'] = $info->getSaveName();
$data['getFilename'] = $info->getFilename();
$data['create_date']= time();
$size = $info->getinfo();
$data['size'] = $size['size'];
// dump($data['size']);die;
$pathName = $data['pathName'];
// dump($pathName);die;
if(file_exists($file_path)) {
$fp = fopen($pathName, "r");
$str = ""; $buffer = 1024;
//每次读取1024 字节
while (!feof($fp)) {
//循环读取,直至读取完整个文件
$str .= fread($fp, $buffer);
}
$str = str_replace("\r\n", "", $str);
// echo $str;
echo (mb_strlen($str,'UTF8'))/1024/3; die;//
} if ($info) {
//插入数据到表
$res = Model('Contact')->insertContact($data);
$this->success('文件上传成功');
} else {
//上传失败获取错误信息
$this->error($file->getError());
}
}