サマリー:<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" &nb
<!doctype html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>文件上傳/下載頁面</title> </head> <body> <form action="upFile.php" method="post" enctype="multipart/form-data" > <label>請選擇需要上傳的文件:</label> <input type="file" name = "upFile"> <br><br> <label> 請確定開始上傳此文件:</label> <input type="submit" value="上傳"> </form> <br><br> <form action="down.php" method="get"> <input type="text" name="downFileName" value="demo.rar"> <input type="submit" value="下載"> </form> </body> </html>
//upFile.php <?php require 'upFun.php'; print_r(upFile($_FILES['upFile'])); ?>
//down.php <?php require 'upFun.php'; $downFile=$_GET['downFileName']; dowFile($downFile); ?>
//upFun.php <?php function dowFile($fileName) { header('Accept-Length:'.filesize($fileName)); // 主要是這一行 header('Content-Disposition:attachment;filename='.basename($fileName)); readfile($fileName); } function upFile($fileInfo,$uploadPath='./upload',$allowExt = ['png','jpg','jpeg','gif' ,'txt','html'],$maxSize = 2000000){ if($fileInfo['error'] ===0){ //返回文件后綴名 $ext = strtolower(pathinfo($fileInfo['name'],PATHINFO_EXTENSION)); if(!in_array($ext,$allowExt)){ return '非法文件類型'; } if(!is_uploaded_file($fileInfo['tmp_name'])){ return '非法上傳!'; } if(!is_dir($uploadPath)){ mkdir($uploadPath,0777,true); } //文件名,用md生成的 $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: case 7: $res = '系統(tǒng)錯誤'; break; } return $res; } }
添削の先生:查無此人添削時間:2019-04-18 09:53:49
先生のまとめ:完成的不錯,上傳文件格式記得要限制一下。不然會有病毒上傳。繼續(xù)加油。