且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-23 18:55:26

这应该对您有用:

(在这里,我首先使用*.csv的所有文件. > glob() .此后,我循环浏览每个文件,并使用 fopen() fgetcsv() .)

(Here I first grab all files out of the directory which have the extension *.csv with glob(). After this I loop through each file and read it with fopen() and 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;
        }

    }

?>