2

我想从另一个目录导入 PHP 中的 excel 文件。我想阅读 excel 并显示它的内容,但它在另一个目录中,我得到了一个错误

Notice: Undefined index: file in F:\Xampp\htdocs\upload.php on line 3

导入.php

<form action="upload.php" >
<input name="file" type="file">
<input name="submit" type="submit">
</form>

上传.php

<?php 
$uploadedStatus = 0; 
echo $_FILES["file"]["name"];
?>
4

2 回答 2

3

为了在 php 中显示 excel,您可以像这样使用PHPExcel

include 'PHPExcel/IOFactory.php';
$inputFileType = 'Excel5';
$inputFileName = 'MyExcelFile.xls';

$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->save('php://output');
exit;

要上传文件,您可以像这样使用简单的文件上传

PHP“上传.php”:

<?php
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $uploadOk = 1;
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        if(isset($_POST["submit"])) {
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
              echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
            } else {
               echo "Sorry, there was an error uploading your file.";
              }
        }
    ?>

HTML 表单:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

更新:

PHPExcel - DEAD PHPExcel 最后一个版本,1.8.1,于 2015 年发布。该项目于 2017 年正式弃用,并于 2019 年永久存档。

该项目已多年未维护,不得再使用。所有用户都必须迁移到其直接继承者 PhpSpreadsheet 或其他替代方案。

于 2016-05-15T10:00:44.130 回答
0

确保您的表单具有enctype处理文件上传的属性。

<form action="upload.php" enctype="multipart/form-data">
    <input name="file" type="file">
    <input name="submit" type="submit">
</form>

参考:

  1. 使用 PHP 导入 excel 文件
  2. PHP Excel 阅读器
于 2016-05-15T09:24:37.650 回答