请帮助。我正在开发的php项目,允许上传电子表格数据,并将其导入到数据库我使用phpSpreadsheet,但我注意到,当我上传.xls,.csv,和.xlsx的$FILE ['']['']是无法识别的,它返回没有文件已附加,但是当我将它转移到另一个项目目录时,我注意到没有使用.htaccess删除.php和.html扩展名,代码就可以工作了。
这里是htaccess
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
这是我的表格
<form action="php/import.php" method="post" enctype="multipart/form-data">
<div class="file-field input-field col s12 m12 l4 xl4">
<div class="btn">
<span>File</span>
<input type="file" name="file_upload">
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
<span class="helper-text <?php echo (!empty($helper_color)) ? $helper_color : ' black-text';?>" data-error="" data-success=""><?php echo (!empty($handler)) ? $handler : 'Note: Only file formats .xls, .csv, and .xlsx';?></span>
</div>
</div>
<div class='input-field col s12 m12 l2 xl2 '>
<input type="submit" name="file_submit" id="file_submit" class='btn'>
</div>
</form>
这是我的进口代码
<?php
require '../../includes/domain_name.php';
require '../../includes/config.php';
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
if(isset($_POST['file_submit'])){
$fileName = $_FILES['file_upload']['name'];
$file_ext = pathinfo($fileName, PATHINFO_EXTENSION);
$allowed_ext = ['xls','csv','xlsx'];
if(in_array($file_ext, $allowed_ext)){
$targetPath = $_FILES['file_upload']['tmp_name'];
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($targetPath);
$data = $spreadsheet->getActiveSheet()->toArray();
$count = 0;
foreach($data as $row){
if($count > 0){
$access_code = $row['0'];
$first_name = $row['1'];
$last_name = $row['2'];
$email = $row['3'];
$sql = "INSERT INTO attendees (access_code, first_name, last_name, email_address) VALUES (?, ?, ?, ?)";
$stmt = mysqli_stmt_init($mysqli);
if(!mysqli_stmt_prepare($stmt,$sql)){
header('location:'.$domain_name.'/admin/login.php?error=stmtfailed');
exit();
}
mysqli_stmt_bind_param($stmt,"isss",$access_code,$first_name, $last_name,$email);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
}else{
$count = 1;
}
}
header('location:'.$domain_name.'admin/attendees.php?error=none');
exit();
}
else{
header('location:'.$domain_name.'admin/attendees.php?error=invalidformat');
exit();
}
}
else{
header('location:'.$domain_name.'admin/attendees.php?error=nofile');
exit();
}
?>
1条答案
按热度按时间mfuanj7w1#
如果您的表单操作如下所示
您的
/upload.php
请求被重定向到/upload
,您将丢失post
数据。由于.htaccess
中的规则,正在进行重定向这是Chrome \ Network中的网络请求
您可以阅读更多关于为什么POST数据不被重定向的信息:
https://softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect