2014-10-05 51 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