简体   繁体   English

Zend Framework 2-如何在Controller中以数组形式显示CSV文件内容

[英]Zend Framework 2 - How to Display CSV file content in Controller as an Array

I am trying to display csv data to array or something as an output using Zend Framework 2 我正在尝试使用Zend Framework 2将csv数据显示为数组或某些内容作为输出

I have created "hello world" module and the controller calls works fine. 我已经创建了“ hello world”模块,并且控制器调用工作正常。

CSV File location is data/csv/Foo.csv CSV档案位置为data / csv / Foo.csv

Below is my contoller : 以下是我的控制器:

public function indexAction()
{
  $filename = 'data/csv/Foo.csv';
  $useFirstRecordAsHeader = true;
  $delimiter = ',';
  $enclosure = '"';
  $escape = '\\';
  $this->file = new SplFileObject($filename);
  $this->file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);

  $this->file->setCsvControl($delimiter, $enclosure, $escape);

  $this->useFirstRecordAsHeader = $useFirstRecordAsHeader;

   return $this;

}

But right now I am getting the error: 但是现在我得到了错误:

SplFileObject::__construct(csv/Foo.csv): failed to open stream: No such file or directory SplFileObject :: __ construct(csv / Foo.csv):无法打开流:没有这样的文件或目录

My csv file is in the same folder controller/csv/Foo.csv 我的csv文件位于同一文件夹controller/csv/Foo.csv

So how to read csv file content and display as output array or any other format? 那么如何读取csv文件内容并显示为输出数组或任何其他格式? I want to do it using Zend Framework 2 only. 我只想使用Zend Framework 2来做。

You are trying to open $this->file = new SplFileObject('csv/Foo.csv'); 您正在尝试打开$this->file = new SplFileObject('csv/Foo.csv'); , since you are using a relative path, on execution time that wont resolve to the folder where your controller is at (it will probably resolve to [yourprojectroot]/csv/Foo.csv ). ,因为您使用的是相对路径,所以执行时间不会解析到控制器所在的文件夹(它可能解析为[yourprojectroot]/csv/Foo.csv )。

If you really wanted to store this csv in controller/csv , you should use something like: 如果您真的想将此csv存储在controller/csv ,则应使用类似以下内容的方法:

 $this->file = new SplFileObject(dirname(__FILE__) . '/csv/Foo.csv');

But, saving that csv there is bad for a several reasons. 但是,将csv保存在那里有几个方面的原因。 First you'd need to grant write permission to your webserver to be able to write in that directory, and you'd be fundamentally messing up with your data/code structure (data and code shouldn't reside together, but in easily separated silos). 首先,您需要向您的Web服务器授予写权限,以便能够在该目录中进行写操作,并且从根本上搞砸了您的数据/代码结构(数据和代码不应放置在一起,而应位于易于分离的筒仓中)。

Better, create a folder "data" and and another folder "csv" in your projects directory, give your webserver permission to write there ( chmod || chown , other methods ), and do something like: 更好的是,在项目目录中创建一个文件夹“ data”和另一个文件夹“ csv”,向您的网络服务器授予在此处写入的权限( chmod || chown ,其他方法 ),然后执行以下操作:

 $file = 'data'. DIRECTORY_SEPARATOR . 'csv' . DIRECTORY_SEPARATOR . 'Foo.csv' ;
 $this->file = new SplFileObject($file );

Besides that, I'm not sure what you are returning actually makes sense. 除此之外,我不确定您返回的内容是否真的有意义。 Try something like: 尝试类似:

public function indexAction()
{
    $filename = 'data' . DIRECTORY_SEPARATOR . 'csv' . DIRECTORY_SEPARATOR . 'Foo.csv';;
    $this->file = new SplFileObject($filename);
    $this->file->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE);

    $this->file->setCsvControl(',', '""', '\\');
    $this->useFirstRecordAsHeader = true;

    $response = $this->getResponse();
    $headers  = $response->getHeaders();
    $headers->addHeaderLine('Content-Type', 'text/csv');

    $contents = $this->file->fread($this->file->getSize());

    $response->setContent($contents);
    return $response;
}

You should not put your csv directory in the Controller directory, as it is not a controller. 您不应将csv目录放在Controller目录中,因为它不是控制器。 It's against the MVC architecture. 这与MVC体系结构背道而驰。 The good practise is to put data in the data directory, under your root directory, at the same level as your module directory. 优良作法是将数据放在根目录下的data目录中,该目录与module目录位于同一级别。

So assuming you have it on this data directory, you can simply write: 因此,假设您在此data目录中拥有它,则可以简单地编写:

$this->file = new SplFileObject('data/csv/Foo.csv');

or better (for portability): 或更好(出于便携性):

$filename = 'data'. DIRECTORY_SEPARATOR . 'csv' . DIRECTORY_SEPARATOR . 'Foo.csv' ;
$this->file = new SplFileObject($filename );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM