upload.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>上传文件</title> </head> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="MyFile"> <input type="submit" value="上传文件"> </form> </body> </html> 上传操作: function.php: <?php /** * 单文件上传操作 * @param $fileInfo 上传的文件信息 * @param string $uploadPath 上传的指定目录 * @param array $allowExt 上传的文件类型 * @param int $maxSize 上传的文件最大值 * @return string 提示信息 */ function upload_file($fileInfo,$uploadPath='./upload',$allowExt=['png','jpg','jpeg','gif','txt','html'],$maxSize=10000000) { if ($fileInfo['error'] === 0){ $ext = strtolower(pathinfo($fileInfo['name'],PATHINFO_EXTENSION)); if (!in_array($ext,$allowExt)){ return '非法文件类型!'; } if ($fileInfo['size']>$maxSize){ return '超出文件上传最大值!'; } if (!is_uploaded_file($fileInfo['tmp_name'])){ return '非法上传!'; } if (!is_dir($uploadPath)){ mkdir($uploadPath,0777,true); } $uniName = md5(uniqid(microtime(true),true)).".".$ext; $dest = $uploadPath."/".$uniName; if (!move_uploaded_file($fileInfo['tmp_name'],$dest)){ return '文件上传失败!'; } 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 = '文件写入失败!'; } return $res; } } upload.php: <?php include 'function10.php'; $fileinfo = $_FILES['MyFile']; var_dump(upload_file($fileinfo)); 下载操作: function.php: <?php /* * 文件的下载 * @param $filename 下载的文件 */ function dow_file($filename) { //告诉浏览器返回文件的大小 header('Accept-Length:'.filesize($filename)); //告诉浏览器文件作为附件处理,并告诉浏览器下载完的文件名 header('Content-Disposition:attachment;filename='.basename($filename)); //输出文件/下载文件 readfile($filename); } dow_file('www.zip');