2014-10-05 175 views
0

请kindlyy帮助out.Am使用php excel与laravel,但每当我尝试导入excel文件到数据库中我得到错误“加载文件”update.xlsx时出错“:无法打开本地主机:9090/xls /update.xlsx阅读!文件不存在。“PHPExcel文件不存在

我的XLS文件夹放在我的公开目录和我装phpexcel与composer.Kindly帮帮忙我ddont知道我提前

在这里做wrong.thanks是我的代码:

<?php 
/************************ YOUR DATABASE CONNECTION START HERE ****************************/ 

define ("DB_HOST", "lhost"); // set database host 
define ("DB_USER", "root"); // set database user 
define ("DB_PASS",""); // set database password 
define ("DB_NAME","name"); // set database name 

// $link = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Couldn't make connection."); 
// $db = mysql_select_db(DB_NAME, $link) or die("Couldn't select database"); 

$databasetable = "applicant"; 

$con = new mysqli(DB_HOST, DB_USER,DB_PASS,DB_NAME); 
/************************ YOUR DATABASE CONNECTION END HERE ****************************/ 


set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/'); 


// This is the file path to be uploaded. 
$inputFileName = asset("xls/".$filename);; 

try { 
    $objPHPExcel = PHPExcel_IOFactory::load($inputFileName); 
} catch(Exception $e) { 
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage()); 
} 


$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); 
$arrayCount = count($allDataInSheet); // Here get total count of row in that Excel sheet 


for($i=2;$i<=$arrayCount;$i++) 
{ 
$surname = trim(strtoupper($allDataInSheet[$i]["A"])); 
$othernames = trim(strtoupper($allDataInSheet[$i]["B"])); 
$address = strtoupper($allDataInSheet[$i]["C"]); 
$lga  = trim(strtoupper($allDataInSheet[$i]["D"])); 
$sex  = trim(strtoupper($allDataInSheet[$i]["E"])); 
$dob  = trim(strtoupper($allDataInSheet[$i]["F"])); 
$genotype = trim(strtoupper($allDataInSheet[$i]["G"])); 
$blood_grp = trim(strtoupper($allDataInSheet[$i]["H"])); 
$phone  = trim(strtoupper($allDataInSheet[$i]["I"])); 
$email  = trim(strtoupper($allDataInSheet[$i]["J"])); 
$occupation = trim(strtoupper($allDataInSheet[$i]["K"])); 
$place_emp = trim(strtoupper($allDataInSheet[$i]["L"])); 
$facility = trim(strtoupper($allDataInSheet[$i]["M"])); 
$medical_his = trim(strtoupper($allDataInSheet[$i]["N"])); 
$allergy = trim(strtoupper($allDataInSheet[$i]["O"])); 
$reg_frm = trim(strtoupper($allDataInSheet[$i]["P"])); 
$reg_to  = trim(strtoupper($allDataInSheet[$i]["Q"])); 
$collector = trim(strtoupper($allDataInSheet[$i]["R"])); 
$form_no = trim(strtoupper($allDataInSheet[$i]["S"])); 
$tell_no = trim(strtoupper($allDataInSheet[$i]["T"])); 
$amt_paid = trim(strtoupper($allDataInSheet[$i]["U"])); 

$query = "SELECT surname FROM `applicant` WHERE `surname` = '$surname' and `othernames` = '$othernames'"; 
$sql = $con->query($query); 
$recResult = mysqli_fetch_array($sql); 
$existName = $recResult["surname"]; 
if($existName=="") { 
$insertTable= $con->query("insert into `applicant` (surname, othernames,address,lga,sex,dob,genotype,blood_grp,phone,email,occupation,place_emp,facility,medical_his,allergy,reg_frm,reg_to,collector,form_no,tell_no,amt_paid) 
    values('".$surname."', '".$othernames."','".$address."','".$lga."','".$sex."','".$dob."', 
     '".$genotype."','".$blood_grp."','".$phone."','".$email."','".$occupation."', 
     '".$place_emp."','".$facility."','".$medical_his."','".$allergy."','".$reg_frm."', 
     '".$reg_to."','".$collector."','".$form_no."','".$tell_no."','".$amt_paid."');"); 


$msg = 'Record has been added'; 
} 
else 
{ 
$msg = 'Record already exist'; 
} 
} 
echo "<div class='alert alert-info'>".$msg."</div>"; 


?> 
+0

请别人帮助我 – 2014-10-05 15:18:13

+0

此代码与Laravel没有任何共同之处。我删除了Laravel标签,因为这不是Laravel代码风格。您应该使用Laravel Excel http://www.maatwebsite.nl/laravel-excel/docs并阅读Laravel文档,了解如何创建Laravel应用程序 – 2014-10-05 16:14:20

+0

我谨慎地不使用laravel blade格式,因为使用DB类或调用使用模型类检索数据不会很容易操作。对于Laravel Excel:它不显示任何关于如何将Excel数据导入数据库的文档。 – 2014-10-05 16:25:50

回答

0

我不确定什么是“作曲家”,以及asset()函数应该做什么,但通常用于将文件上传到PHP脚本,您将使用具有文件输入的“mime/multipart”web表单,然后PHP运行时将使用该文件并使其在$_FILES阵列中可用。阅读PHP manual on handling file uploads了解更多信息。

+0

没有任何共同之处,文件已经上传,asset()函数将该部分返回到public directory.eg asset('img')返回public/img.and你看到我上面发布的函数是一个PHPExcel函数 – 2014-10-05 17:01:43

+0

它看起来像'asset()'返回一个URL,而不是服务器上的本地路径,这是'load()'的期望。如果该文件存在于服务器上,则可以通过其他方式找到该文件。我不知道你用什么来存储上传的文件,而且我也不熟悉Laravel,所以我不能帮助你,对不起 – Guss 2014-10-05 17:07:12

+0

ok tanks.but文件上传到我的公共目录中的文件夹“xls”与一个上传表单 – 2014-10-05 17:10:19

0

PHPExcel无法从URL打开文件,只能从本地文件系统打开文件。由于您使用的网址(localhost)表明该文件位于服务器的文件系统中,因此我们使用了完整的文件系统路径

+0

请示例将帮助 – 2014-10-05 17:02:51

+0

而不是使用'asset(“xls /".$文件名)',使用真正的文件系统路径(例如'/ var/www/html/xls /'。$ filename') web根目录指向 – 2014-10-05 17:05:07

+0

感谢这工作“C:/xampp/htdocs/OYSG/public/xls/update.xlsx”。当我推它到服务器我将重置文件path.thanks再次 – 2014-10-05 17:15:17