PHP中的Excel文件上传
我正在尝试使用PHP上传Excel文件。我使用WAMP服务器和编写的PHP代码将Excel文件上传到同一文件夹中 我保存了一个Excel文件,因此如果我上载保存的文件,那么它工作正常,但是如果我尝试从不同位置(即从桌面)上载另一个Excel文件,则表明PHP中的Excel文件上传,php,excel,Php,Excel,我正在尝试使用PHP上传Excel文件。我使用WAMP服务器和编写的PHP代码将Excel文件上传到同一文件夹中 我保存了一个Excel文件,因此如果我上载保存的文件,那么它工作正常,但是如果我尝试从不同位置(即从桌面)上载另一个Excel文件,则表明abc.xls不可读 我的代码如下: <?php ini_set("display_errors",1); require_once 'excel_reader2.php'; require_once 'db.php'; if(isset(
abc.xls
不可读
我的代码如下:
<?php
ini_set("display_errors",1);
require_once 'excel_reader2.php';
require_once 'db.php';
if(isset($_POST["btnImport"]))
{
if(!empty($_FILES["excelFile"]["tmp_name"]))
{
$fileupload = $_FILES["excelFile"]["name"];
$fileName = explode(".",$_FILES["excelFile"]["name"]);
if($fileName[1]=="xls"||$fileName[1]=="xlsx")
{
$data = new Spreadsheet_Excel_Reader($fileupload);
//echo "Total Sheets in this xls file: ".count($data->sheets)."<br /><br />";
$html="<table border='1'>";
for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
{
if(count(@$data->sheets[$i]['cells'])>0) // checking sheet not empty
{
// echo "Sheet $i:<br /><br />Total rows in sheet $i ".count($data->sheets[$i]['cells'])."<br />";
for($j=1;$j<=count($data->sheets[$i]['cells']);$j++) // loop used to get each row of the sheet
{
$html.="<tr>";
for($k=1;$k<=count($data->sheets[$i]['cells'][$j]);$k++) // This loop is created to get data in a table format.
{
$html.="<td>";
@$html.=$data->sheets[$i]['cells'][$j][$k];
$html.="</td>";
}
@$data->sheets[$i]['cells'][$j][1];
@$ques = mysql_real_escape_string($data->sheets[$i]['cells'][$j][1]);
$opt1 = mysql_real_escape_string($data->sheets[$i]['cells'][$j][2]);
$opt2 = mysql_real_escape_string($data->sheets[$i]['cells'][$j][3]);
$opt3 = mysql_real_escape_string($data->sheets[$i]['cells'][$j][4]);
@$opt4 = mysql_real_escape_string($data->sheets[$i]['cells'][$j][5]);
@$correct = mysql_real_escape_string($data->sheets[$i]['cells'][$j][6]);
@$Level = mysql_real_escape_string($data->sheets[$i]['cells'][$j][7]);
@$Category = mysql_real_escape_string($data->sheets[$i]['cells'][$j][8]);
@$explain = mysql_real_escape_string($data->sheets[$i]['cells'][$j][9]);
$nithi = "INSERT INTO `question` VALUES (NULL,'".$ques."','".$opt1."','".$opt2."','".$opt3."','".$opt4."','".$correct."','".$Level."','".$Category."','".$explain."')";
if (!mysql_query($nithi,$connection))
{
die('Error: ' . mysql_error());
}
$result = mysql_query("SET NAMES utf8");//the main trick
$cmd = "select * from TAMIL";
$result = mysql_query($cmd);
}
}
}}}}
if(@$result){
echo "Questions uploaded successfully";
}
?>
在PHP中上载文件时,会将其放入临时文件夹中
在您的代码中,不需要将上载的文件从临时位置移动到所需位置。相反,您只检查$\u文件[“excelFile”][“tmp\u名称”]
,然后直接使用$\u文件[“excelFile”][“名称”]
,这显然是不正确的
使用移动上传的文件()
功能移动文件,请参阅文档: