时间:2021-07-01 10:21:17 帮助过:23人阅读
if ( $_FILES && $_FILES['attachment'] && $_FILES['attachment']['tmp_name']){ $filesize = $_FILES['attachment']['size']; //文件上传大小限制:5M if($filesize > 5*1024*1024){ exit('上传文件大小超出限制!'); } //文件保存目录,如果不存在则创建之 $uploaddir = ROOT_DIR.'/public/attachment/notice/'; if(!file_exists($uploaddir)) mkdir($uploaddir,0755,true); //获取上传文件扩展名 $arr_file = explode('.', basename($_FILES['attachment']['name'])); $filetype = $arr_file[count($arr_file)-1]; //获取源文件名 $attachment_name = basename($_FILES['attachment']['name']); //对文件进行重命名以避免中文字符的影响 $fname = md5(basename($_FILES['attachment']['name']).microtime()).'.'.$filetype; $uploadfile = $uploaddir . $fname; if (move_uploaded_file($_FILES['attachment']['tmp_name'], $uploadfile)) { //获取文件相对路径 $attachment = '/public/attachment/notice/'. $fname; } else { exit('文件上传失败!'); } }
注:"attachment"是上传文件name,根据项目中的具体属性值修改之。本上传处理未对文件类型做限制处理,可根据具体需求自己处理之。
文件下载:
//输入文件标签 Header ( "Content-type: application/octet-stream" ); Header ( "Accept-Ranges: bytes" ); Header ( "Accept-Length: " . filesize ( $attachment_filepath ) ); Header ( "Content-Disposition: attachment; filename=" . $attachment_filename ); //输出文件内容 //读取文件内容并直接输出到浏览器 echo file_get_contents($attachment_filepath); exit ();