php - 如何将 excel 文件从 <input type ="file"> 上传到 php 服务器

标签 php excel phpexcel

我想使用 php 上传 excel 文件,并想在该 excel 文件中执行一些文件操作。我无法上传文件,如果有人有一些想法请帮助,在服务器端如何获取文件上传的路径?

$target_dir = '上传/';不适合我。我的文件在 D 中:请帮忙。

PHP 代码:

 <?php    
if(isset($_POST['SubmitButton'])){ //check if form was submitted

$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES["filepath"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

require_once dirname(__FILE__) . '/Includes/Classes/PHPExcel/IOFactory.php';

$inputFileType = PHPExcel_IOFactory::identify($target_file);

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

$i=2;
$val=array();
$count=0;
for($i=2;$i<34;$i++)
{
$val[$count++]=$objPHPExcel->getActiveSheet()->getCell('C'.$i)->getValue();
}
//echo'<pre>';print_r($val);
}    
?>

HTML 代码:

<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file"  name="filepath" id="filepath"/></td><td><input type="submit" name="SubmitButton"/>
</body>

最佳答案

您首先需要在读取行之前上传文件:

$target_dir = 'uploads/';
$target_file = $target_dir . basename($_FILES["filepath"]["name"]);
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

move_uploaded_file($_FILES["filepath"]["tmp_name"], $target_file);

// rest of your code...

现在您应该可以继续处理该文件了。 在此之前,该文件从未出现在您的上传文件夹中。

关于php - 如何将 excel 文件从 &lt;input type ="file"> 上传到 php 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38581632/

相关文章:

php_excel07-如何根据单元格的内容固定最小高度和 mak 以增加高度

php - echo 命令不执行任何操作

javascript - 当我在 opencart 中加载时出现错误

excel - Powershell - 将 .XLS 文件转换为 .XLSX

python - 在python pandas中动态跳过excel的顶部空白行

excel - 如何将 Matlab 中的多个列表写入同一个 excel 文件?

php - 如何在PhpSpreadsheet中对齐单元格值?

phpexcel - 在 PHPExcel 中设置背景单元格颜色

php - 如何验证当前日期一位用户的一项条目?

php - 无法安装zend调试器