从目录中读取*.csv文件并显示每个文件的内容失败

Dge*_*000 6 php csv scandir fgetcsv opencsv

我需要帮助阅读文件夹和打开/输出csv数据,我已经使用了其他人写的例子,但没有一个对我有用.

我目前拥有的是这个,但它不输出文件:

$files = scandir($PathToCreate.$version."/"); //scan the folder
foreach($files as $file) { //for each file in the folder

//The following is another example I found but does not output anything I just need to open each file and be able to output / target specific data

$csv = array();
$lines = file($file, FILE_IGNORE_NEW_LINES);

foreach ($lines as $key => $value)
{
    $csv[$key] = str_getcsv($value);
} 
print_r($csv)

}
Run Code Online (Sandbox Code Playgroud)

Riz*_*123 8

这应该适合你:

(在这里我首先抓住所有文件出具有扩展的目录*.csvglob().通过每个文件,这个我循环后,用阅读fopen()fgetcsv().)

<?php

    $files = glob("$PathToCreate$version/*.csv");

    foreach($files as $file) {

        if (($handle = fopen($file, "r")) !== FALSE) {
            echo "<b>Filename: " . basename($file) . "</b><br><br>";
            while (($data = fgetcsv($handle, 4096, ",")) !== FALSE) {
                echo implode("\t", $data);
            }
            echo "<br>";
            fclose($handle);
        } else {
            echo "Could not open file: " . $file;
        }

    }

?>
Run Code Online (Sandbox Code Playgroud)