摘要:<?php //文件上傳操作 function upload_file($fileInfo,$uploadPath="./upload",$allowExt=['txt','png','jpg','jpeg','gif','html','
<?php //文件上傳操作 function upload_file($fileInfo,$uploadPath="./upload",$allowExt=['txt','png','jpg','jpeg','gif','html','php'],$maxSize=1000000) { 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 '非法上傳文件!'; } //判斷需要移動(dòng)到的目錄是否存在 if(!is_dir($uploadPath)){ mkdir($uploadPath,0777,true); } //生成唯一的文件名:uniqid生成唯一的id,microtime 返回當(dāng)前unix時(shí)間戳中的微秒 $uniName=md5(uniqid(microtime(true),true)).".".$ext; //拼接路徑以及文件名 $dest=$uploadPath."/".$uniName; //將文件移動(dòng)到指定路徑 if(!move_uploaded_file($fileInfo['tmp_name'],$dest)){ return '文件上傳失敗!'; }else{ return '文件上傳成功!'; } }else{ switch ($fileInfo['error']){ case 1: $res='上傳的文件超過(guò)了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值!'; break; case 2: $res='上傳文件的大小超過(guò)了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值!'; break; case 3: $res='文件只有部分被上傳!'; break; case 4: $res='沒(méi)有文件被上傳!'; break; case 6: $res='找不到臨時(shí)文件夾'; 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); }
批改老師:西門大官人批改時(shí)間:2019-04-22 10:28:36
老師總結(jié):在企業(yè)開(kāi)發(fā)中,函數(shù)的返回值一般是不直接返回中文字符的。