简体   繁体   English

使用.env变量Lumen获取文件内容

[英]Get file contents using .env variable Lumen

Ok, so I have a .env variable CSV_FILE=~/sites/computer-availability/storage/app/WAV_SUM.csv 好的,我有一个.env变量CSV_FILE =〜/ sites / computer-availability / storage / app / WAV_SUM.csv

I want to load it into my controller so that I can parse it into an array. 我想将它加载到我的控制器中,以便我可以将其解析为一个数组。 I tried env("app.CSV_FILE"); 我试过env(“app.CSV_FILE”); but that just gives me a string of the file path. 但这只是给我一个文件路径的字符串。 Not what I'm wanting. 不是我想要的。 I need the files contents. 我需要文件内容。

How could I go about loading the data from the CSV file using the .env variable? 我怎样才能使用.env变量从CSV文件加载数据?

I have tried the following 我尝试了以下内容

$csv_data = Storage::get(env("WAV_SUM.csv");

$csv_data = env("app.CSV_FILE");

$csv_data = config("app.CSV_FILE");

This is from the official php documentation and slightly altered 这是来自官方的php文档并略有改动

<?php
$row = 1;
$csv = getenv('app.CSV_FILE');
$csv = str_replace("~/sites/computer-availability/","",$csv);
if (($handle = fopen($csv, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "<p> $num fields in line $row: <br /></p>\n";
        $row++;
        for ($c=0; $c < $num; $c++) {
            echo $data[$c] . "<br />\n";
        }
    }
    fclose($handle);
}
?>

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

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