abstract:文件的上傳//通過判斷上傳文件,將文件移動至目標(biāo)文件夾,并為此文件進(jìn)行唯一命名,無論上傳成功和失敗均返回相應(yīng)的信息 function dn_ul_file($uploadinfo,$upload_maxsize=10000,$upload_tpye=['txt','png','jpg'],$up_load='./uplo
文件的上傳
//通過判斷上傳文件,將文件移動至目標(biāo)文件夾,并為此文件進(jìn)行唯一命名,無論上傳成功和失敗均返回相應(yīng)的信息 function dn_ul_file($uploadinfo,$upload_maxsize=10000,$upload_tpye=['txt','png','jpg'],$up_load='./upload'){ //上傳 判斷上傳文件的大小,類型是否 if ($uploadinfo['error']==0){ $uploadinfo_type=strtolower(pathinfo($uploadinfo['name'],PATHINFO_EXTENSION)); if (!in_array($uploadinfo_type,$upload_tpye)){ return'上傳類型為非法文件類型'; } if ($uploadinfo['size']>$upload_maxsize){ return'上傳文件超過文件最大上傳量'; } //判斷是通過http中的post方法進(jìn)行上傳 if (!is_uploaded_file($uploadinfo['tmp_name'])){ return'非法上傳操作'; } //判斷上傳文件夾是否存在,如果不存在則創(chuàng)建 if (!is_dir($up_load)){ mkdir($up_load,0777,true); } //將上傳文件命名為唯一的命名,uniqid()生產(chǎn)唯一的id,microtime()返回當(dāng)前unix時間蹉中的微秒數(shù) $uniname=md5(uniqid(microtime(true),true)).".".$uploadinfo['name']; //拼接文件及文件名 $dest=$up_load."/".$uniname; //將文件移動至目標(biāo)文件夾 if (move_uploaded_file($uploadinfo['tmp_name'],$dest)){ return'文件上傳成功'; }else {return'文件上傳失敗';} } else { switch( $uploadinfo['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; }
前端頁面
<!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" name="上傳"><!--上傳按鈕--> </form> </body> </html>
調(diào)用
include 'demo5.php';//引入上傳的方法庫 $fileinfo=$_FILES['MyFile'];//將上數(shù)組賦值給$fileinfo echo dn_ul_file($fileinfo);//調(diào)用上傳方法
文件的下載
function down_file($filename){ //告訴瀏覽器返回文件的大小 header('Accept-Length'.filesize($filename)); //告訴瀏覽器文件作為附件處理,并告訴瀏覽器下載完畢的文件名 header('Content-Disposition:attachment;filename='.basename($filename)); //輸出文件 readfile($filename); } down_file('www1.zip');
Correcting teacher:查無此人Correction time:2019-06-05 09:44:33
Teacher's summary:完成的不錯。封裝方法,多看看別人寫好的,把好的拿過來。繼續(xù)加油