Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从目录中读取*.csv文件并显示每个文件的内容失败_Php_Csv_Fgetcsv_Opencsv_Scandir - Fatal编程技术网

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

Php 从目录中读取*.csv文件并显示每个文件的内容失败,php,csv,fgetcsv,opencsv,scandir,Php,Csv,Fgetcsv,Opencsv,Scandir,我需要阅读文件夹和打开/输出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

我需要阅读文件夹和打开/输出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)

}

这应该适合您:

(在这里,我首先从目录中抓取扩展名为
*.csv
的所有文件,然后循环遍历每个文件,并使用和读取它们。)


第一个问题可能是您必须忽略两个目录项
,这两个目录项具有特殊意义,对您没有用处:

$files = scandir($PathToCreate.$version."/"); //scan the folder
foreach($files as $file) { //for each file in the folder
  if ( ! in_array($file, ['.','..'])) {
    $lines = file($file, FILE_IGNORE_NEW_LINES);
    foreach ($lines as $key => $value) {
      $csv[$key] = str_getcsv($value);
    } 
    print_r($csv)
  }
}

您的csv文件有什么分隔符?“它们都不起作用”没有帮助。您的问题是什么?它使用逗号分隔信息和您现在得到的输出以及您希望得到什么?顺便说一句:看看
fgetcsv()
函数:1。检查每个文件是否既不是
也不是
对于整个循环不是很清楚2<代码>$csv=fgetcsv()什么?@Rizier123关于2:抱歉,意外粘贴:-(谢谢你指出这一点。@Rizier123关于1:“不是很清楚”?当然有多种方法可以做到这一点,你使用的是
glob
,也很好。但是“不清楚”是什么呢在这种情况下?假设我们有10k个文件。那么检查每个文件是否既不是
也不是
在循环之前,最好使用
数组_diff()
对它们进行过滤,这也有助于提高性能!@Rizier123我怀疑这是个问题:-)OP提到上传的文件。他会很忙地上传10k文件:-)但从理论上看你是对的。天哪,这太完美了。谢谢你,有没有办法通过标题来定位特定数据?@Dgeorgex000000不客气!(有没有办法通过标题来定位特定数据?你的确切意思是什么?)xxx@Dgeorgex000000而不是:
echo内爆(“\t”美元数据)do
回显$data[3]$files = scandir($PathToCreate.$version."/"); //scan the folder
foreach($files as $file) { //for each file in the folder
  if ( ! in_array($file, ['.','..'])) {
    $lines = file($file, FILE_IGNORE_NEW_LINES);
    foreach ($lines as $key => $value) {
      $csv[$key] = str_getcsv($value);
    } 
    print_r($csv)
  }
}