猿问

无法打开流:没有这样的文件或目录 Codeignter

我正在尝试将数据从 csv 文件上传到我的 sql server 数据库。


所以我创建了这个 csv 阅读器库:


<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');


class CSVReader

{


    var $fields;

    /** columns names retrieved after parsing */

    var $separator = ';';

    /** separator used to explode each line */

    var $enclosure = '"';

    /** enclosure used to decorate each field */


    var $max_row_size = 4096;

    /** maximum row size to be used for decoding */


    function parse_file($p_Filepath)

    {


        $file = fopen($p_Filepath, 'r');

        $this->fields = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure);

        $keys_values = explode(',', $this->fields[0]);


        $content    =   array();

        $keys   =   $this->escape_string($keys_values);


        $i  =   1;

        while (($row = fgetcsv($file, $this->max_row_size, $this->separator, $this->enclosure)) != false) {

            if ($row != null) { // skip empty lines

                $values =   explode(',', $row[0]);

                if (count($keys) == count($values)) {

                    $arr    =   array();

                    $new_values =   array();

                    $new_values =   $this->escape_string($values);

                    for ($j = 0; $j < count($keys); $j++) {

                        if ($keys[$j] != "") {

                            $arr[$keys[$j]] =   $new_values[$j];

                        }

                    }


                    $content[$i] =   $arr;

                    $i++;

                }

            }

        }

        fclose($file);

        return $content;

    }


    function escape_string($data)

    {

        $result =   array();

        foreach ($data as $row) {

            $result[]   =   str_replace('"', '', $row);

        }

        return $result;

    }

}

?>


然后我试图从文件中开始对我的数据检索部分进行编码,以便我可以将它们发送到模型以便将它们插入到数据库中。


但我收到此错误:


无法打开流:没有这样的文件或目录


这是指向fopen指令。


你能帮我解决这个问题吗?


开心每一天1111
浏览 136回答 1
1回答

函数式编程

因为您没有指向完整路径(包括文件夹)尝试<?php&nbsp;$data = $this->upload->data();&nbsp; &nbsp; //$csvFile=FCPATH."upload/".$fileName; //php way (uploaded filepath and filename )&nbsp; $csvFile=$data['full_path']; //codeigniter way&nbsp; $result =&nbsp; &nbsp;$this->csvreader->parse_file($csvFile);?>
随时随地看视频慕课网APP
我要回答