无法打开流:没有此类文件或目录Codeignter

时间:2019-07-31 09:21:50

标签: php codeigniter csv

我正在尝试将csv文件中的数据上传到我的sql服务器数据库中。

所以我创建了这个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;
    }
}
?>

并且我添加了这部分JS代码以将文件从视图发送到控制器:

$("#form").on('submit', (function(e) {
    e.preventDefault();

    $.ajax({
        url: "../Commande/sendToIncadeaWithFile",
        type: "POST",
        data: new FormData(this),
        contentType: false,
        cache: false,
        processData: false,
        success: function() {

        },
        error: function(e) {

        }
    });
}));

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

我刚开始尝试通过执行以下操作来查看是否正确检索了数据:

<?php
if ($_FILES['file']) {

    $fileName = $_FILES["file"]["name"];
    var_dump($_FILES["file"]["size"]);
    $cofing['upload_path'] = './upload/';
    $cofing['allowed_types'] = 'csv';
    $this->load->library('upload', $cofing);

    if (!$this->upload->do_upload('file')) {
        echo $this->upload->display_errors();
    } else {
        $data = $this->upload->data();

        $result =   $this->csvreader->parse_file($fileName);
        var_dump($result);
    }
}
?>

但我遇到此错误:

  

打开流失败:没有这样的文件或目录

指向fopen指令。

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

1 个答案:

答案 0 :(得分:1)

因为您没有指向完整路径(包括文件夹)

尝试

<?php
 $data = $this->upload->data();
    //$csvFile=FCPATH."upload/".$fileName; //php way (uploaded filepath and filename )
  $csvFile=$data['full_path']; //codeigniter way
  $result =   $this->csvreader->parse_file($csvFile);

?>