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

问题描述:

I have a project where a user uploads a photo with a name and caption, and I am having trouble with displaying the name and caption.

Right now, it's displaying the entire text file with each image, and I am having trouble fixing this.

My code so far:

    <?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 />
";  
            }
        }
        closedir($DirOpen);         
    ?>

Any help would be greatly appreciated.

我有一个项目,用户上传带有名称和标题的照片,而我在显示 名称和标题。 p>

现在,它正在显示每个图像的整个文本文件,而我无法修复此问题。 p>

到目前为止我的代码: p>

 &lt;?php 
 $ Dir =“files”; 
 $ DirEntries = scandir(  $ Dir); 
 foreach($ DirEntries as $ Entry)
 {
 if((strcmp($ Entry,'。')!= 0)&amp;&amp;(strcmp($ Entry,'..')  != 0))
 {
 echo“&lt; p&gt;名称:”。  file_get_contents(“imagelist.txt”)。  “&lt; / p&gt;”; 
 echo“&lt; a href = \”files /“。$ Entry。”\“target = \”_ blank \“&gt;”  。  $入场。  “&lt; / a&gt;&lt; br /&gt; 
”;  
} 
} 
 closedir($ DirOpen);  
?&gt; 
  code>  pre> 
 
 

非常感谢任何帮助。 p> div>

You can use 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);