我想将userdata上传到mysql数据库。为此,我想使用php将excel数据导入mysql数据库。我写了代码。但我面临一个错误。我提供我的代码供参考。我的代码如下。
include 'config.php';
require_once 'Excel/reader.php';
$allowedExts = array("xls","xlsx");
$temp = explode(".", $_FILES["file"]["name"]);
if (($_FILES["file"]["size"] < 20000)&& (in_array($extension, $allowedExts)))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$filename=$_FILES["file"]["name"] ;
$filetype=$_FILES["file"]["type"] ;
$filesize=$_FILES["file"]["size"] ;
$filetemp=$_FILES["file"]["tmp_name"];
//////////////////////////
$row=0;
$handle = fopen($filetemp, "r");
$data = new Spreadsheet_Excel_Reader();
$data->read($filetemp);
我的代码停止从这一行执行:
$data = new Spreadsheet_Excel_Reader();
我在屏幕上设置了这个错误: php28E.tmp不可读。
请在这个问题上帮助我。
答案 0 :(得分:0)
PHP-manual说(http://www.php.net/manual/en/features.file-upload.post-method.php):
默认情况下,文件将存储在服务器的默认临时目录中,除非在php.ini中使用upload_tmp_dir指令给出了另一个位置。
[...]
无论逻辑如何,您都应该从临时目录中删除该文件或将其移动到其他位置。
[...]
在使用之前,您应该使用move_uploaded_file()移动上传的文件。
答案 1 :(得分:0)
您可以使用phpexcel,完美运行并且还支持所有类型的电子表格格式,并且已有详细记录。 在PhpExcel Home下载。 谷歌上有很多提示和教程。
此代码适用于我:
<?php
$newPath = __DIR__.'/'.basename($_FILES['filepath']['tmp_name']);
move_uploaded_file($_FILES['filepath']['tmp_name'], $newPath);
$objPHPExcel = PHPExcel_IOFactory::load($newPath);
............................