简体   繁体   中英

CodeIgniter file Uploading Class, error with do_upload

I want to program the function to upload excel files to a sql db. I have a problem, because the $this->upload->do_upload('file') don't execute and I cant figure out why.

Here is my Controller:

function import(){
    $this->breadcrumbs[] = array(
           "title" => "Import",
           "link" => "interview/import"
       );
    $this->template->load("interview_import", $this->breadcrumbs, $data);
}

function import_data() {         
    $this->load->library("form_validation");        
    $config = array(
        'upload_path' => FCPATH. 'upload/',
        'allowed_types' => 'xls'
        );
    $this->load->library('upload', $config);
    if($this->upload->do_upload('file')) {
        $data = $this->upload->data();
        @chmod($data['full_path'], 0777);
    }
    $this->load->library("Spreadsheet_Excel_Reader");
    $this->spreadsheet_excel_reader->setOutputEncoding('CP1251');

    $this->spreadsheet_excel_reader->read($data['full_path']);
    $sheets = $this->spreadsheet_excel_reader->sheets[0];
    error_reporting(E_ALL ^ E_NOTICE);

    $data_excel = array();
    for ($i = 2; $i <= $sheets[0]['numRows']; $i++) {
        if ($sheets['cells'][$i][1] == '') {
            break;
        }
        $data_excel[$i -1]['name'] = $sheets['cells'][$i][1];
        $data_excel[$i -1]['phone'] = $sheets['cells'][$i][2];
        $data_excel[$i -1]['address'] = $sheets['cells'][$i][3];

        /*for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
            echo "\"".$data->sheets[0]['cells'][$i][$j]."\",";
        }
    echo "\n";*/
    }
    echo '<pre>';
    print_r($data_excel);
    echo '</pre>';
    die();       
}

And this is my View:

<?php    
      echo form_open_multipart('interview/import_data');
      echo form_upload('file');
      echo '<br/>';
      echo form_submit(null, 'Upload');
      echo form_close();
?>

Do you have any idea what I am doing wrong?

When trying to upload I get the error message:

> A PHP Error was encountered Severity: Notice Message: Undefined variable: data Filename: controllers/Interview.php Line Number: 521 Backtrace: File: C:\\Users\\A66155193\\Desktop\\xampp\\htdocs\\yitt\\application\\controllers\\Interview.php Line: 521 Function: _error_handler File: C:\\Users\\A66155193\\Desktop\\xampp\\htdocs\\yitt\\index.php Line: 311 Function: require_once

So obviously the if-clause can execute because:

Undefined variable: data

Thank you for your help!

After if condition of do_upload check that if the file is not uploading than whats the error

if($this->upload->do_upload('file')) {
    $data = $this->upload->data();
    @chmod($data['full_path'], 0777);
} else{
    $error = array('error' => $this->upload->display_errors());
    print_r($error);
}

You have to check it because $data variable is initializing in if statement and at the bottom its saying undefine a variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM