我有一个.xlsx文件和一个html文件,其中包含一个。我只需要上传它,并将它发送到一个php文件(使用js或任何其他方式)。
php文件需要一个.xlsx文件(出于这个原因,我不解析.xlsx)。如果我直接将它加载到php文件中,它可以很好地工作,但我需要通过用户界面上传,在本例中是html视图。
致以问候。
更新:
现在.html看起来是这样的:
<div class="MainContainerPrice">
<form action="php/excel_to_mysql.php" method="POST">
<input type="file" name="excel" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>
<input type="submit">
</form>
</div>.php看起来是这样的:
<?php
include 'simplexlsx.class.php';
$file = $_FILES['excel'];
$xlsx = new SimpleXLSX('pricesExcel.xlsx'); //the file directly uploaded that I need to send from html.
...
?>但现在我有了下一个错误:
未定义索引:第2行的...\excel_to_mysql.php中的excel。
为什么不认识这个名字?
发布于 2019-06-15 20:44:28
您需要对html和PHP部分做一些调整
<div class="MainContainerPrice">
<form action="php/excel_to_mysql.php" method="POST" enctype="multipart/form-data">
<input type="file" name="excel" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>
<input type="submit">
</form>
</div>注意enctype="multipart/form-data"。这是真正发送文件所需要的。
PHP文件中的
<?php
include 'simplexlsx.class.php';
$file = $_FILES['excel']['tmp_name'];
$xlsx = new SimpleXLSX($file); //the file directly uploaded that I need to send from html.
...
?>$_FILES['excel']['tmp_name']包含上传文件的完整路径,请注意,您不能依赖具有'.xlsx‘扩展名的名称,因为出于安全目的,该文件将获得一个随机的名称。
我强烈建议您在临时目录中使用该文件,并在使用后删除它。
如果SimpleXLSX类需要“.xlsx”扩展名才能正常工作,则可以尝试将其添加到临时文件中
rename($_FILES['excel']['tmp_name'],$_FILES['excel']['tmp_name'].'.xlsx');https://stackoverflow.com/questions/56613659
复制相似问题