Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法通过CSV将数据上传到codeigniter中的数据库_Php_Codeigniter_Csv - Fatal编程技术网

Php 无法通过CSV将数据上传到codeigniter中的数据库

Php 无法通过CSV将数据上传到codeigniter中的数据库,php,codeigniter,csv,Php,Codeigniter,Csv,这是我正在使用的图书馆 class Csvimport { private $filepath = ""; private $handle = ""; private $column_headers = ""; /** * Function that parses a CSV file and returns results * as an array. * * @access public * @param filepath string Location

这是我正在使用的图书馆

class Csvimport {

private $filepath = "";
private $handle = "";
private $column_headers = "";

/**
 * Function that parses a CSV file and returns results
 * as an array.
 *
 * @access  public
 * @param   filepath        string  Location of the CSV file
 * @param   column_headers  array   Alternate values that will be used for array keys instead of first line of CSV
 * @param   detect_line_endings  boolean  When true sets the php INI settings to allow script to detect line endings. Needed for CSV files created on Macs.
 * @return  array
 */
public function get_array($filepath='', $column_headers='', $detect_line_endings=FALSE)
{   
    // If true, auto detect row endings
    if($detect_line_endings){
        ini_set("auto_detect_line_endings", TRUE);
    }

    // If file exists, set filepath
    if(file_exists($filepath))
    {
        $this->_set_filepath($filepath);
    }
    else
    {
        return FALSE;            
    }

    // If column headers provided, set them
    $this->_set_column_headers($column_headers);

    // Open the CSV for reading
    $this->_get_handle();

    $row = 0;

    while (($data = fgetcsv($this->handle, 0, ",")) !== FALSE) 
    {   
        // If first row, parse for column_headers
        if($row == 0)
        {
            // If column_headers already provided, use them
            if($this->column_headers)
            {
                foreach ($this->column_headers as $key => $value)
                {
                    $column_headers[$key] = trim($value);
                }
            }
            else // Parse first row for column_headers to use
            {
                foreach ($data as $key => $value)
                {
                    $column_headers[$key] = trim($value);
                }                
            }          
        }
        else
        {
            $new_row = $row - 1; // needed so that the returned array starts at 0 instead of 1
            foreach($column_headers as $key => $value) // assumes there are as many columns as their are title columns
            {
                $result[$new_row][$value] = trim($data[$key]);
            }
        }
        $row++;
    }

    $this->_close_csv();

    return $result;
}

/**
 * Sets the filepath of a given CSV file
 *
 * @access  private
 * @param   filepath    string  Location of the CSV file
 * @return  void
 */
private function _set_filepath($filepath)
{
    $this->filepath = $filepath;
}

/**
 * Sets the alternate column headers that will be used when creating the array
 *
 * @access  private
 * @param   column_headers  array   Alternate column_headers that will be used instead of first line of CSV
 * @return  void
 */
private function _set_column_headers($column_headers='')
{
    if(is_array($column_headers) && !empty($column_headers))
    {
        $this->column_headers = $column_headers;
    }
}

/**
 * Opens the CSV file for parsing
 *
 * @access  private
 * @return  void
 */
private function _get_handle()
{
    $this->handle = fopen($this->filepath, "r");
}

/**
 * Closes the CSV file when complete
 *
 * @access  private
 * @return  array
 */
private function _close_csv()
{
    fclose($this->handle);
}    
}

这是控制器

public function importcsv() {
    $data['addressbook'] = $this->account_model->get_all();
    $data['error'] = '';    //initialize image upload error array to empty

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'csv';
    $config['max_size'] = '10000';

    $this->load->library('upload', $config);


    // If upload failed, display error
    if (!$this->upload->do_upload()) {
        $data['error'] = $this->upload->display_errors();

        $this->load->view('tenant/import', $data);


    } else {
        $file_data = $this->upload->data();
        $file_path =  '../uploads/'.$file_data['file_name'];



        if ($this->csvimport->get_array($file_path)) {
            $csv_array = $this->csvimport->get_array($file_path);


            foreach ($csv_array as $row) {
                $insert_data = array(

                    'first_name'=>$row['first_name'],
                    'last_name'=>$row['last_name'],
                    'postal_address'=>$row['postal_address'],
                    'company_name'=>$row['company_name'],
                    'company_url'=>$row['company_url'],
                    'office_hours'=>$row['office_hours'],
                    'industry'=>$row['industry'],
                    'contact_type'=>$row['contact_type'],   
                    'status'=>$row['status'], 
                );
                $this->account_model->insert($insert_data);
            }
            $this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
            redirect(base_url('tenant/import'));

        } else 
            $data['error'] = "Error occured";


        }




    } 
如果我在$data上打印,它会打印出发生错误,这意味着它会自动转到我的else状态,文件将上载到upload文件夹,因此这意味着问题出在第二条if语句上,有人能帮我确定第二条if语句哪里出错了吗?

更改为:

$file_path =  $file_data['full_path'];
更改为:

$file_path =  $file_data['full_path'];

错误是什么?没有显示错误消息,它是我的if语句的一部分,用于显示上载失败时发生的错误参考此链接并尝试回显错误:错误是什么?没有显示错误消息,它是我的if语句的一部分,用于显示上载失败时发生的错误参考此链接并尝试回显错误: