试图让用户从他们的计算机中选择一个浏览过的 excel 文件。获取该excel文件并对其进行解析,然后将数据写入数据库。我无法让解析工作。我知道我需要使用...
<form enctype="multipart/form-data" action="uploader.php" method="POST">
Choose a file to upload: <input name="uploadedfile" type="file" id="uploadedfile" /> <br />
<input type="submit" value="Upload File" />
我唯一的问题是,当我使用 $_FILES["uploadedfile"]["name"] 时,它只给我文件名而不是文件目录,所以我怎样才能将它传递给 PHPExcel Reader。这不只是文件名的字符串而不是文件路径吗?
这是我在 uploader.php 中的内容:
<?php
include ('/PHPExcel/Classes/PHPExcel/IOFactory.php');
$filename = $_FILES["uploadedfile"]['name'];
echo $filename;
$inputFileType = PHPExcel_IOFactory::identify($filename);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader -> load($filename);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$data = array();
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
$data[] = $rowdata;
// Insert row data array into your database of choice here
}
foreach ($data as $param){
echo $param;
}
?>