将文件的上传下载操作封装成一个单独的方法

Original 2019-04-21 14:50:17 217
abstract:
$maxSize){ return '超出文件上传最大值'; } //判断文件上传方式 if(!is_uploaded_file($fileInfo['tmp_name'])){ return '非法上传文件!'; } //判断需要移动到的目录是否存在 if(!is_dir($uploadPath)){ mkdir($uploadPath,0777,true); } //生成唯一的文件名:uniqid生成唯一的id,microtime 返回当前unix时间戳中的微秒 $uniName=md5(uniqid(microtime(true),true)).".".$ext; //拼接路径以及文件名 $dest=$uploadPath."/".$uniName; //将文件移动到指定路径 if(!move_uploaded_file($fileInfo['tmp_name'],$dest)){ return '文件上传失败!'; }else{ return '文件上传成功!'; } }else{ switch ($fileInfo['error']){ case 1: $res='上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值!'; break; case 2: $res='上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值!'; break; case 3: $res='文件只有部分被上传!'; break; case 4: $res='没有文件被上传!'; break; case 6: $res='找不到临时文件夹'; break; case 7: $res='文件写入失败'; break; } } return $res; } //文件下载操作 function dow_file($fileName){ //告诉浏览器返回文件的大小 header('Accept-Length:'.filesize($fileName)); //告诉浏览器文件作为附件处理,并告诉浏览器文件下载完的文件名 header('Content-Disposition:attachment;fileName='.basename($fileName)); //输出文件 readfile($fileName); }


Correcting teacher:西门大官人Correction time:2019-04-22 10:28:36
Teacher's summary:在企业开发中,函数的返回值一般是不直接返回中文字符的。

Release Notes

Popular Entries