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

时间:2013-09-09 18:53:50

标签: php arrays text-files

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

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

我建立了一个网站来帮助一些在家上学的学生,但是想要将这个功能添加到网站上,以帮助他们查看所有过去,现在和将来的课程。我知道一点点PHP,但不能把它包裹起来,似乎我越努力,我就越困惑。这对我来说是一次学习经历。

我尝试过使用fopen,但只能打开文本文件而不是整个目录。我原以为我需要获取目录列表,将其放入数组中,并使用fopen打开数组中的每个文件,但我可能会离开。非常感谢任何指导我正确方向的帮助!

3 个答案:

答案 0 :(得分:1)

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

//从数组中的给定目录获取文件列表(数组将包含文件名)。    //推荐使用,文件名为完整路径,或者是脚本的相对路径

$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。

答案 1 :(得分:0)

你可以通过两种方式使用glob来扫描目录,或者更好的Directory Iterator 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>';

?>

答案 2 :(得分:0)

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

$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);
}
相关问题