在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($_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"; } ?> 

我怎样才能从不同的位置上传Excel文件?

当一个file upload到PHP,它被放入临时文件夹。

在您的代码中,没有要求将上传的文件从临时位置移动到所需的位置。 相反,你只是检查$_FILES["excelFile"]["tmp_name"] ,然后直接使用$_FILES["excelFile"]["name"]这显然是不正确的。

使用move_uploaded_file()函数来移动文件,请参阅文档: http : //php.net/move_uploaded_file