<form action="{:U('Index/files')}" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file"/> <br/> <input type="submit" name="submit" value="Submit"/> </form>
public function files(){ $filename = $_FILES['file']['name']; $ext = preg_split("/\./", $filename); $ext = strtolower($ext[1]); /* * 获取文件后缀名 */ $allowed_types = array("xls", "xlsx"); $filePath = "./Public/excel/" . $_FILES["file"]["name"]; // print_r($filePath);die; if (!in_array($ext, $allowed_types)) { echo "File type is wrong!"; die; } // else if (file_exists($filePath)) { // echo "A file with this name already exists!"; Die //; ; if (! $ PHPReader-> // } else { move_uploaded_file ($ _ the FILES [ "File"] [ "tmp_name"], $ filePath); } / ** * @param $ Val * @return String * Check the file name meets the requirements, if met, will be saved to the specified path. If not, an error. * / // Import ( "Org.Util.PHPExcel"); require_once "./ThinkPHP/Library/Org/Util/PHPExcel/Classes/PHPExcel.php"; // $ filePath the FILES = $ _ [ "File"] [ " tmp_name "]; // SLEEP (50); $ PHPExcel = new new \ PHPExcel (); // default read excel with excel2007, if the format is wrong, then read with a previous version of the $ PHPReader = new \ PHPExcel_Reader_Excel2007 () PHPReader new new = $ \ PHPExcel_Reader_Excel5 (); (! $ PHPReader-> canRead ($ filePath)) IF { return $ this-> error (null, "NO File"); } } $ = $ PHPReader- PHPExcel> Load ($ filePath); $ SheetCount = $ PHPExcel-> getSheetCount (); $ sheetNames = $ PHPExcel-> getSheetNames (); // var_dump ($ sheetNames); // Die; / ** read excel file how many sheet * / $ objExcel = Array (); for ($ SheetID = 0; $ SheetID <$ SheetCount; $ SheetID ++) { / ** read excel worksheet file * / $ name = $ sheetNames [$ SheetID]; $currentSheet = $PHPExcel->getSheetByName($name); $name = iconv("utf-8", "gb2312", $name); /**取得最大的列号*/ $allColumn = $currentSheet->getHighestColumn(); /**取得一共有多少行*/ $allRow = $currentSheet->getHighestRow(); for ($currentRow = 1; $currentRow <= $allRow; $currentRow++) { /**从第A列开始输出*/ for ($currentColumn = 'A'; $currentColumn <= $allColumn; $currentColumn++) { $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65, $currentRow)->getValue(); $val = iconv("utf-8", "gb2312", $val); $objExcel[$name][$currentRow - 1][ord($currentColumn) - 65] = $val; // coding needs to be converted into gb2312 / * $ Val = urlencode ($ Val); $ objExcel [$ name] [$ currentRow -. 1] [the ord ($ also currentColumn) - 65] = urldecoded ($ Val); * / } } } The unlink ($ filePath); / * * $ objExcel json_encode = ($ objExcel); * $ objExcel urldecoded = ($ objExcel); * read the contents of the file, because json_encode () parameters must be utf-8 encoded. In order to ensure that the results output characters, where the first contents of the array is encoded with urlencode, * after being () json_encode encoded, then the decoding urldecode. * / objExcel return $ //; // print_r ($ objExcel); the foreach ($ objExcel AS = K $> $ V) { // remove the inside id spliced into a new one-dimensional array $ array_reduce Result = (V $, 'The array_merge', Array ()); // filter out null withdrawn inside id $ id = array_filter ($ Result); } }