从目录读取* .csv文件并显示每个文件的内容失败 [英] Reading *.csv files from directory and showing the content of each file fails
本文介绍了从目录读取* .csv文件并显示每个文件的内容失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要阅读文件夹并打开/输出csv数据的帮助,我使用了其他人写的示例,但没有一个对我有用.
I need help with reading the folder and opening / outputting the csv data, I have used examples other people have written but none of them have worked for me.
我目前所拥有的是这个,但是它不输出文件:
What I currently have is this, but it doesn't output the files:
$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)
}
推荐答案
这应该对您有用:
(在这里,我首先使用*.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;
}
}
?>
这篇关于从目录读取* .csv文件并显示每个文件的内容失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文