如何一次获取一行文件的内容


How can I get the contents of a file one line at a time?

我有一个项目,其中用户上传了带有名称和标题的照片,我在显示名称和标题时遇到问题。

现在,它显示每个图像的整个文本文件,我无法解决这个问题。

到目前为止我的代码:

    <?php
        $Dir = "files";
        $DirEntries = scandir($Dir);
        foreach ($DirEntries as $Entry)
        {
            if((strcmp($Entry, '.') != 0) && (strcmp($Entry, '..') != 0))
            {
                echo "<p>Name: " . file_get_contents("imagelist.txt") . "</p>";
                echo "<a href='"files/" . $Entry . "'" target='"_blank'" >" . $Entry . "</a><br />'n";  
            }
        }
        closedir($DirOpen);         
    ?>

任何帮助将不胜感激。

您可以使用

fgets()

$inputFile = fopen("file.txt", "r");
if ($inputFile) {
    while (($line = fgets($inputFile)) !== false) {
        echo $line."<br>";
        // The process read the file line by line        
    }
} else {
    echo "There was an error in the opening file";
} 
fclose($inputFile);