请帮助out.ym使用php excel与laravel但是每当我尝试将excel文件导入数据库时我都会收到错误"加载文件时出错" update.xlsx":无法打开localhost: 9090 / xls / update.xlsx供阅读!文件不存在。"。
我的xls文件夹放在我的公共目录中,并且正在使用composer加载phpexcel.Kindly帮助我知道出错了什么。谢谢提前
这是我的代码:
<?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 :(得分:0)
我不确定什么是“composer”以及asset()
函数应该做什么,但通常对于PHP脚本的文件上传,你使用“mime / multipart”web表单一个文件输入,然后PHP运行时将使用该文件并使其在$_FILES
数组中可用。有关详细信息,请阅读PHP manual on handling file uploads。
答案 1 :(得分:0)
PHPExcel无法仅通过本地文件系统从URL打开文件。由于您正在使用的网址(localhost
)表明该文件位于服务器的文件系统上,因此我们建议使用完整的文件系统路径