读取目录中每个文件的每一行文本并放入数组中


Read each line of text of each file in a directory and place into array?

>我有一个目录,里面有文本文件,每天都会添加新的文本文件。每个文本文件都是一个包含 4 行文本的学校课程。第 1 行是课程编号,第 2 行是课程标题,第 3 行是描述,第 4 行是截止日期。

在PHP中,我需要能够读取所有当前和未来的文本文件并将它们放入HTML表中。 表中标有"课程编号"、"课程标题"、"描述"和"截止日期"的 4 列。每个文本文件为 1 个表格行。

我创建了一个网站来帮助一些在家上学的学生,但想将此功能添加到网站上,以帮助他们查看所有过去、现在和未来的课程。我知道一点PHP,但无法理解它,似乎我尝试得越多,我就越困惑。这对我来说是一次学习经历。

我尝试使用fopen但只能让它打开文本文件而不是整个目录。我想我需要获取一个目录列表,将其放入数组中,然后使用fopen打开数组中的每个文件,但我可能离得很远。非常感谢任何帮助为我指明正确的方向!

你的方法是这样做的一种方式。您可以扫描目录中所需的文件,并使用 file() 函数检索数组中的文件内容。我只会发布部分代码,因为从目录中获取文件名是显而易见的(请参阅其他答案中的glob())。

/

/get 来自数组中给定目录的文件列表(数组将包含文件名)。 建议文件名具有完整路径或脚本的相对路径

$task_array = Array();
foreach ($filelist as $filename)
{
    try
    {
        $file_content = file($filename); // we get an array with this function
        // you could do this the other way, by using fopen() and fread(), but this is easier
    }
    catch(Exception $e)
    {
        $(file_content = false;
    }
    if (($file_content !== false) && (!empty($file_content)))
    {
        $task_array[] = $file_content;
    }
}

您的任务数组将变成二维数组,如下所示:

Array(
    [0] -> Array(
        [0] -> 1
        [1] -> 'Lesson Title'
        [2] -> 'Lesson Description here'
        [3] -> '2013-09-25'
    )
    [1] -> Array(
        [0] -> 2
        [1] -> 'Lesson Title 2'
        [2] -> 'Lesson 2 Description here'
        [3] -> '2013-09-25'
    )
)

然后,当你有这个数组时,你可以再次使用 foreach 以 HTML 显示它。

但是,如果您想以正确的方式执行此操作,则应使用数据库,例如MySQL。

你可以用两种方式,要么使用将扫描目录的glob,要么使用更好的目录迭代器 http://php.net/manual/en/class.directoryiterator.php,我建议使用glob方法更容易一些,所以病了。

这取决于您是否已经存储了以前的记录,但无论哪种方式,请尝试在此处获取一些示例。

我现在已经改进并测试了下面的代码,因为我写的另一个是垃圾

<?php
/**
 * @author - Sephedo
 * @for - Randall @ Stackoverflow
 * @question - http://stackoverflow.com/questions/18704981/read-each-line-of-text-of-each-file-in-a-directory-and-place-into-array/18705231#18705231
 */
$directory = 'lessons/'; // The directory to the lesson text files
$linesToReturn = 4; // Set to four for the number of lines in each text file ( for expansion? )
// Find all files in the directory which are .txt files
foreach( glob( $directory . "*.txt" ) as $filename )
{
    // Open the file
    if( $handle = @fopen( $filename, "r") )
    {
        $x = 0; // Start the line counter
        // Cycle each line until end or reach the lines to return limit
        while(! feof( $handle ) or $x < $linesToReturn )
        {
            $line = fgets($handle); // Read the line
            $lessons[$filename][] = $line;
            $x++; // Increase the counter
        }
        // This lines makes sure that are exactly 4 lines in each lesson
        if( count( $lessons[$filename] ) != $linesToReturn ) unset( $lessons[$filename] );
    }
}
// creates a blank list if no files or valid files were found.
if(! isset( $lessons ) ) $lessons = array();
// The rest of the page just builds a simple table to display each lesson.-
echo '<h1>Lesson Plans</h1>';
echo '<table>';
echo '<th>Lesson Number</th><th>Lesson Title</th><th>Description</th><th>Due Date</th>';
foreach( $lessons as $file => $details )
{
    echo '<tr><td>' . $details[0] . '</td><td>' . $details[1] . '</td><td>' . $details[2] . '</td><td>' . $details[3] . '</td></tr>';
}
echo '</table>';
?>

这就是我遍历目录和读取文件的方式:

$dir = "/YOUR_DIRECTORY_PATH/*";
foreach(glob($dir) as $file) { //load each file in the directory
    $fileHandle = fopen($file, "r");
    while (!feof($fileHandle)) {   // load each line
        $line = fgets($fileHandle);
        echo $line . '<br />';
    }
    fclose($fileHandle);
}