我想将excel文件上传到一个文件夹,然后将文件名上传到数据库中,但它给了我一个带有网址的空白页面 “http://localhost/myweb.com/admin/upload?file=tes+upload.xlsx”。 “tes upload”是文件名。该文件未上传到该文件夹以及数据库。我不知道我错过了什么。我将文件夹权限设置为777。
我的表格:
<form action="<?php echo site_url('admin/upload')?>" enctype="multipart/form-data">
<input type="file" name="file" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
<input type="submit" class="btn btn-success" value="submit">
</form>
我的控制器:
function upload()
{
if (!empty($_FILES))
{
$filename = $_FILES["file"]["name"];
$file_basename = substr($filename, 0, strripos($filename, '.'));
$file_ext = substr($filename, strripos($filename, '.'));
$tempFile = $_FILES['file']['tmp_name'];
$data = "admin".uniqid().$file_ext;
$targetPath = getcwd() . '/uploads/';
$targetFile = $targetPath . $data ;
move_uploaded_file($tempFile, $targetFile);
$file_path = FCPATH.'/uploads/';
require_once APPPATH."/third_party/PHPExcel.php";
require_once APPPATH . "/third_party/PHPExcel/IOFactory.php";
$inputFileName = $file_path;
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet); // Here get total count of row in that Excel sheet
$regex = "~\d{5}~";
$dataexcel = Array();
for($i=1;$i<=count($dataexcel);$i++)
{
$dataexcel[$i]['to_name']=$allDataInSheet[$i]["A"];
$dataexcel[$i]['to_address']=$allDataInSheet[$i]["B"];
$dataexcel[$i]['to_phone']=$allDataInSheet[$i]["C"];
preg_match($regex, $allDataInSheet[$i]["B"], $result);
$dataexcel['to_zipcode'] = $result[0];
};
$data_user = array(
'status' => '1',
'filename_admin' => $data,
);
$this->load->model('excel');
$this->excel->upload_excel($allDataInSheet,$data_user);
}
}
我的模特:
function upload_excel($allDataInSheet)
{
$regex = "~\d{5}~";
array_shift($allDataInSheet);
foreach ($allDataInSheet as $key)
{
preg_match($regex, $key['B'], $result);
$data = array(
'request_id' => $request_id,
'to_name' => $key['A'],
'to_phone' => $key['C'],
'to_address' => $key['B'],
'to_zipcode' => $result[0],
'tariff' => '0'
);
$this->db->insert('excel', $data);
$this->db->update('request',$request_id);
}
}
请帮帮我。提前致谢