如何将数组放入目录中的项目?

问题描述:

我有一个包含大约2000个文本文档的目录,我想遍历每个解析数据。我怎样才能做到这一点?如何将数组放入目录中的项目?

scandir()将带来所有文件名到数组中。

阵列SCANDIR(字符串$目录 [摘要$ sorting_order = 0,资源$背景]])

<?php 

    $dir = '/tmp'; 
    $files1 = scandir($dir); 
    $files2 = scandir($dir, 1); 

    print_r($files1); 
    print_r($files2); 

?> 
+0

谢谢乔纳森! – John 2009-12-01 18:25:54

+0

本例来自PHP网站,请提供适当的信用! – Michael 2009-12-01 18:28:12

+0

谢谢乔纳森。我相信这是我需要的。 – John 2009-12-01 18:30:23

你为什么不检查DirectoryIterator页面上的PHP手册?尼斯类

http://php.net/manual/en/class.directoryiterator.php

其余的都是小事..

+0

感谢链接和rereas – John 2009-12-01 18:27:22

我假设你有兴趣在PHP这样做。您将要使用的关键功能是scandir函数和file_get_contents函数。

所以,你是源将是这个样子:

<?php 
    $my_dir_path = "/path/to/my/dir"; 
    $files = scandir($my_dir_path); 
    $files_contents_to_array = new Array(); // will contain a mapping of file name => file contents 
    if($files && count($files) > 0) { 
    for($files as $file) { 
     if($file /* some pattern check, verify it is indeed the file you need */) { 
     $files_contents_to_array[$file] = file_get_contents($file); 
     } 
    } 
    } 
?> 

我认为这可能是你在找什么。