2014-11-23 44 views
0

我有一個項目,用戶上傳帶有名稱和標題的照片,而我無法顯示名稱和標題。如何一次獲取一行文件的內容?

現在,它顯示每個圖像的整個文本文件,我無法解決這個問題。

我迄今爲止代碼:

<?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);   
    ?> 

任何幫助將不勝感激。

+2

不要使用file_get_contents()函數,它不會將整個文件讀入一個巨大的字符串.....要麼使用[文件()](HTTP:// php.net/manual/en/function.file.php)返回一個數組,每行作爲一個單獨的數組條目,或者用[fopen()]手動打開文件(http://php.net/manual/ en/function.fopen.php)並使用[fgets()](http://php.net/manual/en/function.fgets.php)遍歷它一次讀取一行 – 2014-11-23 23:48:23

回答

1

您可以使用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);