Public functionActionimport () {
//post asked for it.$fileName=$_files[' File '] [' Name ']; $fileTmpName=$_files[' File '] [' Tmp_name ']; //determine if the uploaded file is selected if(Empty($fileName)) { $data[' message '] = "Please select a file to upload"; return $this->render ("Batch_add",$data); } //determine if the file you choose to upload is not in CSV format if(Explode(".",$fileName) [1]! = "CSV") { $data[' message '] = "Please select File Upload in CSV format"; return $this->render ("Batch_add",$data); } //create an empty array, pre-release ImageUrl $imageCollection= []; //Open the file you want to read $handle=fopen($fileTmpName, ' R '); //Parse CSV file while(!feof($handle)) { //Fgets method read by line $result=fgets($handle); //determine if each row read has a value if(!Empty($result)) { $arrResult=Explode(",",$result); $name=$arrResult[0]; $age=$arrResult[1]; $gender=$arrResult[2]; //the original path of the picture $imagePath=$arrResult[3]; //the name of the picture $image=basename($imagePath); $data[' message '] = Person::saveperson ($name,$age,$gender,$image); if($data[' message '] = = ' Add successful ') { //Place the URI of each picture in the array Array_push($imageCollection,$imagePath); } } } //Close File Stream fclose($handle); //after you close the file stream before uploading the picture, note that streams and streams are not nested if(!Empty($imageCollection)) { foreach($imageCollection as $value) { //upload the picture to the server Move_uploaded_file($imagePath,dirname(__dir__). '/web/images/'.Date("YMD"). ' /‘.$image); } } return $this->render ("Batch_add",$data); }
PHP parsing. csv file