上传
public function enclosureUploadAction()
{
$dir = $_SERVER['DOCUMENT_ROOT'] . "/upload/work_flie/";
$file_info = $_FILES['file'];
$file_error = $file_info['error'];
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
$arr = [
'info' => '',
'status' => -1,
'url' => ''
];
if ($file_error == 0) {
$file_only = time() . '-' . mt_rand(100, 99999) . '-' . $file_info['name'];
if (move_uploaded_file($file_info['tmp_name'], $dir . $file_only)) {
$file_path = '/upload/work_file/' . $file_only;
$arr['info'] = '上传成功';
$arr['status'] = 200;
$arr['url'] = $file_path;
$work_closure_model = new WorkenclosureModel();
$file_res = $work_closure_model->where('file_path = ?', $file_path)->select()->fetchAll();
if ($file_res) {
$arr['info'] = '该文件已经存在在数据库了,不能重复添加';
$this->ajaxReturn($arr);
}
$work_closure_model->work_id = 0;
$work_closure_model->file_path = $file_path;
$work_closure_model->status = 0;
$work_closure_model->c_time = time();
$work_closure_model->add();
} else {
$arr['info'] = '上传失败';
}
$this->ajaxReturn($arr);
} else {
switch ($file_error) {
case 1:
$arr['info'] = '上传文件超过了PHP配置文件中upload_max_filesize选项的值';
break;
case 2:
$arr['info'] = '超过了表单max_file_size限制的大小';
break;
case 3:
$arr['info'] = '文件部分被上传';
break;
case 4:
$arr['info'] = '没有选择上传文件';
break;
case 6:
$arr['info'] = '没有找到临时文件';
break;
case 7:
case 8:
$arr['info'] = '系统错误';
break;
}
$this->ajaxReturn($arr);
}
}
取消上传
public function cancelEnclosureUploadAction()
{
$file_name = get('file_name', null);
$arr = [
'info' => '',
'status' => -1,
];
if (empty($file_name)) {
$arr['info'] = '请选择取消上传的文件';
$this->ajaxReturn($arr);
}
$dir = $_SERVER['DOCUMENT_ROOT'] . "/upload/work_flie/";
$del_name = $dir . $file_name;
if (file_exists($del_name)) {
$res = unlink($del_name);
if ($res) {
$arr['status'] = 200;
$arr['info'] = '删除成功';
$name = "/upload/work_flie/".$file_name;
$work_closure_model = new WorkenclosureModel();
$work_closure_model->where('file_path = ?', $name)->delete();
} else {
$arr['info'] = '操作失误导致文件无法删除或文件无法删除';
}
} else {
$arr['info'] = '无法到该文件或者已经被删除了';
}
$this->ajaxReturn($arr);
}
下载
public function downloadEnclosureAction()
{
$file = get('file_name',null);
if(!$file){
return '请传入文件名';
}
$file_lj = $_SERVER['DOCUMENT_ROOT'] . "/upload/work_flie/";
$files = $file_lj . $file;
if (!file_exists($files)) {
return "文件不存在";
} else {
$file1 = fopen($files, "r");
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: " . filesize($files));
Header("Content-Disposition: attachment; filename=" . $file);
echo fread($file1, filesize($files));
fclose($file1);
}
}