File upload:
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('文件上传失败!'); } }
Note: "attachment" is the name of the uploaded file, which is modified according to the specific attribute value in the project. This upload process does not impose restrictions on file types and can be processed by yourself according to specific needs.
File download:
//输入文件标签 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 ();