php 将 .csv 文件读入 Laravel 中的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40136331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:07:29  来源:igfitidea点击:

Read a .csv file into an array in Laravel

phplaravellaravel-5

提问by Andre

I have stored a .csv file in this directory in Laravel 5 :

我在 Laravel 5 的这个目录中存储了一个 .csv 文件:

/storage/app/data.csv

/storage/app/data.csv

Inside of this function I am trying to read the contents of this file and extract a few columns from it, like name and city to an array, sample code:

在这个函数内部,我试图读取这个文件的内容并从中提取几列,比如名称和城市到一个数组,示例代码:

use Illuminate\Support\Facades\Storage;

public function get_data() {

 $file_n = Storage::url('data.csv');
 $file = fopen($file_n, "r");
 $all_data = array();
 while ( ($data = fgetcsv($file, 200, ",")) !==FALSE {

     $name = $data[0];
     $city = $data[1];
     $all_data = $name. " ".$city;

     array_push($array, $all_data);
  }
  fclose($file);
}

I am getting an ErrorException in Controller:

我在控制器中收到一个 ErrorException:

fopen(/storage/data.csv): failed to open stream: No such file or directory

But the file is there. Is there a better way to do this instead?

但是文件在那里。有没有更好的方法来做到这一点?

Thanks for looking!

感谢您的关注!

回答by Valkzer

Your issue is with the path as previous people pointed out.

您的问题与前人指出的路径有关。

You can use the helper function storage_path https://laravel.com/docs/5.3/helpers#method-storage-path

您可以使用辅助函数 storage_path https://laravel.com/docs/5.3/helpers#method-storage-path

In your case it will be used like this:

在您的情况下,它将像这样使用:

storage_path('app/data.csv');

As a recomendation you can use this libary to work with CSV/Excel files, it's pretty easy to use: https://github.com/Maatwebsite/Laravel-Excel

作为推荐,您可以使用此库来处理 CSV/Excel 文件,它非常易于使用:https: //github.com/Maatwebsite/Laravel-Excel

回答by UserHelpNeeding02356

Laravel 5.7try this :

Laravel 5.7试试这个:

Storage::disk('local')->path('import.csv');

It should be good.

应该不错。

回答by Andre

I figured it out by using "storage_path".

我通过使用“storage_path”来解决这个问题。

...
$filename = storage_path('/app/data.csv');
$file = fopen($filename, "r");
$all_data = array();
while ( ($data = fgetcsv($file, 200, ",")) !==FALSE ) {
    ...
}

Thanks!

谢谢!

回答by derivative

I solved this issue by adding a url key to my local disk config in config/filesystems.php like so:

我通过向 config/filesystems.php 中的本地磁盘配置添加一个 url 键来解决这个问题,如下所示:

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
        'url' => storage_path('app')
    ],

Now I can reference Storage::url('filename.txt') and it returns:

现在我可以引用 Storage::url('filename.txt') 并返回:

    /home/josh/apps/storage/app/filename.txt

回答by Anu Alex

Try the below

试试下面的

$file_n = Storage::get('data.csv');