Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
185 views
in Technique[技术] by (71.8m points)

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

I have a directory with text files in it and new text files getting added each day. Each text file is a school lesson with 4 lines of text. Line 1 is Lesson Number, line 2 is Lesson Title, line 3 is Description, and line 4 is Due Date.

I need, in PHP, to be able to read all current and future text files and place them into an HTML table. 4 columns in a table labeled Lesson Number, Lesson Title, Description, and Due Date. Each text file being 1 table row.

I've made a site to help out some homeschooled students but wanted to add this functionality to the site to help them view all past, present, and future lessons. I know a little PHP but can't wrap my head around it and it seems the more I try the more I'm getting confused. This is a learning experience for me.

I've tried using fopen but can only get it to open a text file and not a whole directory. I was thinking I need to get a directory listing, place that into an array, and use fopen to open each file in the array but I may be way off. Any help to point me in the right direction is greatly appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your approach is one way of doing it. You could scan the directory for the files you need, and use the file() function to retrieve file contents in an array. I will only post partial code, as getting file names from a directory is obvious (see glob() in other answers).

//got file list from a given directory in an array (array would contain file names). //it is recommanded, that file names to be with full path, or a relative path to the script

$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;
    }
}

Your task array will become a two-dimensional array, like this:

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'
    )
)

Then, when you have this array, you could use foreach again, to display it in HTML.

However, if you would want to do this the right way, you should use a database, for example MySQL.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...