Cakephp 3.0使用fget读取本地文件内容

时间:2016-07-15 13:50:42

标签: php csv upload cakephp-3.0

我已经计算出将数据从.csv文件上传到数据库所需的PHP代码,而不是使用CakePHP3.x,但我正在努力将其集成到我的框架中。我不想将文件上传到数据库,只读取CSV文件中的数据。

这是我的代码,可以在localhost上将数据上传到我的数据库。

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$fp = fopen("CSV_Upload_Test.csv", "r");

while( !feof($fp)) {
    if( !$line = fgetcsv($fp, 1000, ',', '"')) {
        continue;
    }

    $mysqlimport = "INSERT INTO divisiontwosix.csvtestsystems VALUES('".$line[0]."','".$line[1]."','".$line[2]."','".$line[3]."','".$line[4]."')";
    mysql_query($mysqlimport) or die(mysql_error());
}

fclose($fp);

这是我在我的服务器上安装CakePHP的代码,但我似乎无法找到(或者更有可能要求找到正确的问题)关于如何实现它的答案。

这是我的控制器:

   public function upload()
    {
        $system = $this->Systems->newEntity();
        //Check if file has been uploaded.
        if(!empty($this->request->data['Systems']['upload']['name']))
            {
                $file = $this->request->data['Systems']['upload'];
            }
        if ($this->request->is('post')) {
            $system = $this->Systems->patchEntity($system, $this->request->data);
//          add upload data to controller function upload
            $file = $_REQUEST['text'];
            $fp = fopen("$file","r");
            while( !feof($fp)) {
    if( !$line = fgetcsv($fp, 1000, ',', '"')) {
        continue;
    }

    $mysqlimport = "INSERT INTO divtwosix.systems VALUES('".$line[0]."','".$line[1]."','".$line[2]."','".$line[3]."','".$line[4]."')";
    mysql_query($mysqlimport) or die(mysql_error());
}

fclose($fp);
            if ($this->Systems->save($system)) {
                $this->Flash->success(__('The system has been saved.'));
                return $this->redirect(['action' => 'index']);
            } else {
                $this->Flash->error(__('The system could not be saved. Please, try again.'));
            }
        }
        $estimates = $this->Systems->Estimates->find('list', ['limit' => 200]);
        $this->set(compact('system', 'estimates'));
        $this->set('_serialize', ['system']);
    }

这是我的表格(upload.ctp),你可以看到我尝试过几件事:

<?= $this->Form->create($system) ?>
    <fieldset>
        <legend><?= __('Upload System') ?></legend>
        <?php
            echo $this->Form->create('User', array('url' => array('action' => 'create'), 'enctype' => 'multipart/form-data'));
//          echo $this->Form->create($system, ['type'=>'file']);
            echo $this->Form->input('estimates_id', ['label'=>'Select Estimate Name','options' => $estimates]);
//          echo $this->Form->input('file', ['label'=>'Upload works with CSV files only','type' => 'file']);
            echo $this->Form->input('text', ['label'=>'Input File location']);
        ?>
    </fieldset>
    <?= $this->Form->button(__('Upload')) ?>

我得到的错误是

fopen(C:\Users\jrogers\Desktop\Book1.csv): failed to open stream: No such file or directory [APP/Controller/SystemsController.php, line 86]

fopen(Book1.csv): failed to open stream: No such file or directory [APP/Controller/SystemsController.php, line 86]

我不打算将文件添加到数据库,而只是将.csv文件中的数据添加到数据库中。我意识到我可能遗漏了一些简单的东西,但非常感谢你的帮助。我找到的示例仅显示如何将文件上载到数据库,而不是如何从本地文件读取数据。在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在遗留的PHP脚本中,上传的文件应该通过$ _FILES数组访问,所以你应该像这个例子那样修改你的代码

    $file = $_FILES['text']['tmp_name'];
    $fp = fopen($file,"r");
祝你好运

来源:http://php.net/manual/en/features.file-upload.post-method.php

注意:我不是蛋糕用户,但IMO应该有正确的方式来访问上传的文件