Urgent help! Codeigniter cannot call PHPExcel. I am a beginner in Codeigniter. I used the file upload class to upload csv or excel files. at the same time, I used PHPExcel to read the content and load it into 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'] = '000000 ';
$ 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', 'decimal', '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 files and Excel folders. the folders are PHPExcel. php and PHPExcel. 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 want to call the GetArrTable_CVS method to read the data in the table and put it in the array table to return the result. Why can't I use require_once here? How can I call PHPExcel? Please advise. 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
Make sure the path is correct.
At the beginning of the file, the entire program cannot run. I found it strange where these two sentences cannot be run.
The path should be correct. it is in the same directory. Please check it out.
Error message!
Where can I find the error message?
If require_once or require fails, an error message is required.
This depends on the settings of your php. ini.
If display_errors = On, it is displayed On the page.
I set it as you said, but there is no error message. Is it necessary to write a statement in the program that displays the error message?
This problem has been solved. my god, I have tried many methods !!! Big Head. Thanks to the moderator.