0

试图让用户从他们的计算机中选择一个浏览过的 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;
}

?>
4

2 回答 2

0

您将需要临时名称来阅读它或将其移动到您选择的位置:

print_r($_FILES);
于 2013-10-08T20:01:12.183 回答
0

直接指向路径并将其传递给 IOFactory:

$filename = $_FILES["uploadedfile"]['name'];
$fullFilename = PROJECT_DIR . 'upload/' . $filename;

$inputFileType = PHPExcel_IOFactory::identify($fullFilename);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader -> load($fullFilename);
于 2017-03-02T09:41:21.540 回答