I am a beginner codeigniter, using its file upload class to upload a CSV or Excel file, while using Phpexcel to read the contents and mount the database. My program is as follows:
controllers/products.php file:
$memsn = $this->session->userdata (' memsn ');
$this->load->library (' Excel_read_operat ');
$groups = Array (' purchaser ', ' salesman ', ' viewer ', ' merchant ');
if ($this->ion_auth->in_group ($groups))
{
$this->session->set_flashdata (' message ', $this->lang->line ("access_denied"));
$data [' message '] = (validation_errors ()? Validation_errors (): $this->session->flashdata (' message '));
Redirect (' module=products ', ' Refresh ');
}
$this->form_validation->set_rules (' UserFile ', $this->lang->line ("Upload_file"), ' Xss_clean ');
if ($this->form_validation->run () = = True)
{
if (Isset ($_files["UserFile")) {
$this->load->library (' Upload_photo ');
$dest _dir = ' uploads/'. $memsn. ' /';
$this->dest_dir_fortest = $dest _dir;
if ($this->upload_photo->direct_is_exists ($dest _dir)) {
}else{
Redirect ("Module=products&view=upload_csv", ' Refresh ');
}
$config [' upload_path '] = $dest _dir;
$config [' allowed_types '] = ' csv|xls|xlsx ';
$config [' max_size '] = ' 2048 ';
$config [' overwrite '] = TRUE;
$old _file_name = $_files["UserFile" ["Name"];
$new _file_name = $this->getsystemtime (). $_files["UserFile" ["Name"];
$config [' file_name '] = $new _file_name;
Initialize
$this->upload_photo->initialize ($config);
if (! $this->upload_photo->do_upload ()) {
echo the Errors
$error = $this->upload_photo->display_errors ();
$this->session->set_flashdata (' message ', $error. $dest _dir);
Redirect ("Module=products&view=upload_csv", ' Refresh ');
}
if ($this->upload_photo->file_ext = = = '. csv ') {
$csv = $this->upload_photo->file_name;
Excel_read_operat::initialized ($dest _dir. $new _file_name);
$phpexcel _csv_arr_table = Excel_read_operat::getarrtable_cvs ();
if (!empty ($phpexcel _csv_arr_table)) {
$keys = Array (' Code ', ' name ', ' category_id ', ' STATUS ', ' DESCRIPTION ', ' decdesction ', ' custprice ', ' LENGTH ', ' HEIGHT ', ' WIDTH ', ' STATUS ');
$final = Array ();
foreach ($phpexcel _csv_arr_table as $row _csv_value) {
$final [] = Array_combine ($keys, $value);
foreach ($final as $csv _pr) {
if ($this->products_model->getproductbycode ($csv _pr[' Code ')) {
$this->session->set_flashdata (' message ', $this->lang->line ("Check_product_code"). " (". $csv _pr[' code ']."). ". $this->lang->line (" code_already_exist "));
Redirect ("Module=products&view=upload_csv", ' Refresh ');
}
}
}
Under Libraries/, there are excel_read_operat.php class files, and Excel folders, which are phpexcel.php and Phpexcel folders under Folders. The Excel_read_operat.php class is as follows:
if (! defined (' BasePath ')) exit (' No Direct script access allowed ');
Class Excel_read_operat {
Public $_filepath;
Private $_arraytable;
Private $_feature;
public static Function initialized ($filepath) {
if (file_exists ($filePath)) {
$this->_filepath = $filepath;
}else{
return Array ();
}
}
public static Function GetData ($val) {
$JD = GREGORIANTOJD (1, 1, 1970);
$gregorian = Jdtogregorian ($jd + intval ($val)-25569);
return $gregorian;
}
public static Function Getarrtable_cvs () {
Require_once ' excel/phpexcel.php ';
Require_once ' excel/phpexcel/iofactory.php ';
$PHPReader _csv = new Phpexcel_reader_csv ();
$file _ext=strtolower (PathInfo ($this->_filepath, pathinfo_extension));
$PHPReader _csv->setinputencoding (' GBK ');
$PHPReader _csv->setdelimiter (', ');
if (! $PHPReader _csv->canread ($this->_filepath)) {
return Array ();
}
$PHPExcel = $PHPReader _csv->load ($this->_filepath);
$currentSheet = $PHPExcel->getsheet (0);
$allColumn = $currentSheet->gethighestcolumn ();
$allColumn = phpexcel_cell::columnindexfromstring ($allColumn);
$allRow = $currentSheet->gethighestrow ();
Echo ' Row is: '. $allRow. '
';
Echo ' Allcolumn is: '. $allColumn. '
';
for ($currentRow = 2; $currentRow <= $allRow; $currentRow + +) {
for ($currentColumn = 0; $currentColumn < $allColumn; $currentColumn + +) {
$val = $currentSheet->getcellbycolumnandrow ($currentColumn, $currentRow)->getvalue ();
$this->_feature[$currentColumn] = $val;
}
$this->_arraytable [$currentRow] = $this->_feature;
}
echo "\ n";
return $this->_arraytable;
}
public static Function Getarrtable_excel () {
Require_once ' excel/phpexcel.php ';
Require_once ' excel/phpexcel/iofactory.php ';
$PHPReader = new phpexcel_reader_excel2007 ();
if (! $PHPReader->canread ($this->_filepath)) {
$PHPReader = new Phpexcel_reader_excel5 ();
if (! $PHPReader->canread ($this->_filepath)) {
Echo ' no Excel ';
Return
}
}
$PHPExcel = $PHPReader->load ($this->_filepath);
$currentSheet = $PHPExcel->getsheet (0);
$allColumn = $currentSheet->gethighestcolumn ();
$allColumn = phpexcel_cell::columnindexfromstring ($allColumn);
$allRow = $currentSheet->gethighestrow ();
Echo ' Row is: '. $allRow. '
';
Echo ' Allcolumn is: '. $allColumn. '
';
for ($currentRow = 2; $currentRow <= $allRow; $currentRow + +) {
for ($currentColumn = 0; $currentColumn < $allColumn; $currentColumn + +) {
$val = $currentSheet->getcellbycolumnandrow ($currentColumn, $currentRow)->getvalue ();
$this->_feature[$currentColumn] = $val;
}
$this->_arraytable [$currentRow] = $this->_feature;
}
echo "\ n";
return $this->_arraytable;
}
}
?>
I'm going to call Getarrtable_cvs this method, read the data in the table, and put it back in the array table, why can't I use require_once here? How am I supposed to call Phpexcel? Please advise me. Thank you
Reply to discussion (solution)
Require_once ' excel/phpexcel.php ';
Require_once ' excel/phpexcel/iofactory.php ';
Should be placed at the beginning of the program file
and verify that the path is correct
At the beginning of the file, the entire program will not run. I found these two sentences where not to run, too strange.
The path should be right, in the same directory, trouble you to see
Give the error message!
Excuse me, where do I find the wrong information?
Require_once, require failure must have the wrong message
This is with where to look, to see your php.ini settings
If Display_errors = On is displayed in the page
I followed your settings, but no error message came out. Do you want to write a statement that shows the error message in the program?
This problem has been solved, God, tried a lot of many ways!!! Head big. But thanks for the moderator.