【问题标题】:CakePHP How to get data from csv file after uploadCakePHP如何在上传后从csv文件中获取数据
【发布时间】:2017-05-30 13:09:57
【问题描述】:

我创建了一个输入类型为file 的表单,按钮提交以访问控制器中的功能,问题是我无法从该文件中获取数据。 当我打电话时在控制器中:

function displayAll() {

	$adherents = $_FILES['file']['tmp_name'];
	prd($adherents);
}

我发现了这个错误

未定义索引:文件 [APP\controllers\adherents.php,第 36 行]

这是视图:

<h2>Upload filer</h2>
<br>
<fieldset class="pano bleu">

<?php echo $this->Form->create('adherentsSuph', array('action'=>'displayAll'));?>

	<?php
		echo $this->Form->input('file', array('type' => 'file'));
		echo $bouton->block(array(
            'cancel'=>array('action'=>'index'),
            'submit'=>array('id'=>'addAdherentFile')
    ));
		echo $this->Form->end();?>
</fieldset>

【问题讨论】:

  • 请始终提及您的确切 CakePHP 版本(vendor/cakephp/cakephp/VERSION.txt 或 lib/Cake/VERSION.txt 中的最后一行)
  • 我在 CakePHP 1.3 下工作

标签: cakephp cakephp-1.3


【解决方案1】:

'type' =&gt; 'file' 添加到您的表单选项中,如下所示:

&lt;?php echo $this-&gt;Form-&gt;create('adherentsSuph', array('action'=&gt;'displayAll', 'type' =&gt; 'file')); ?>

检查是否是有效请求和有效文件,然后使用file()将整个文件读入一个数组并循环。

if (!empty($this->data)) {
    if (is_file($this->data['file']['tmp_name'])) {
        $file = file($this->data['file']['tmp_name']);
        foreach ($file as $key => $row) {
            var_dump($row); die;
        }
    }
}

另请参阅:

调试$this-&gt;data以检查是否需要将$this-&gt;data['file']更改为$this-&gt;data['adherentsSuph']['file']

【讨论】:

  • 首先感谢您的帮助,但它不起作用我发现了这个错误:未定义的属性:AdherentsSuphsController::$request 致命错误:在 C:\wamp64 中调用一个成员函数 is() on null \www\project\controllers\adherents_suphs_controller.php 在第 35 行
  • 我现在更新了 CakePHP 1.3 的答案,请再次检查。
  • 总是 $this->数据为空!!我试图显示像这样的所有数据 prd($this->data) ===> no result
【解决方案2】:

您的问题质量很差,因为您提供的代码不完整。无论如何,我会尽力帮助你。

请检查以下事项:

  1. 您使用的是 CakePHP FormHelper
  2. 如果是这样,请确保使用'type' =&gt; 'file' 初始化Form(请参阅here)。
  3. 最后检查一下,$this-&gt;request-&gt;dataController 中得到了什么。

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 1970-01-01
    • 2013-05-12
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多