//上传附件 public function actionUploadFile(){ $outputMsg = null; $insertAtt = []; if(!isset($_FILES['file'])){ return $this->outPut([11,'未接收到正确的文件']); } //保存文件 foreach ($_FILES['file']['tmp_name'] as $key =>$v){ if(is_uploaded_file($_FILES['file']['tmp_name'][$key])){ //is_uploaded_file() 函数判断指定的文件是否是通过 HTTP POST 上传的。 $fileTmpName = $_FILES['file']['tmp_name'][$key]; $fileName = $_FILES['file']['name'][$key]; $extensionName = strrchr($fileName,'.'); //strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符。 //验证文件合法 if(!in_array($extensionName,\Yii::$app->params['uploadFileExtend'])){ return $this->outPut([11,$extensionName.'格式文件不允许上传!']); } $saveFileName = "date_".time()."_r_".rand(0,9999)."{$extensionName}"; //$saveFilePath = "upload_file/".date('Y',time())."_".date('m',time())."_".date('d',time()); $saveFilePath = "upload_file/".date('Y_m_d',time()); if(!file_exists($saveFilePath) && !mkdir($saveFilePath)){ return $this->outPut([11,'目录创建失败']); } $saveFilePath = $saveFilePath."/".$saveFileName; if(move_uploaded_file($fileTmpName,$saveFilePath)){ $insertAtt[] = [ 'file_name'=>$fileName, 'file_path'=>$saveFilePath ]; } } } return $this->outPut([0],[$insertAtt]); }
[moka同学笔记]PHP上传文件
猜你喜欢
转载自blog.csdn.net/Mr_Moka/article/details/108251819
今日推荐
周排行