2013-04-13 25 views
-2

我已將記事本/文本文件存儲在文檔根目錄中。我沒有將該文件保存在數據庫中。請告訴我如何獲取文件的文件名並按順序顯示文件。由於如何使用PHP從本地服務器顯示記事本/文本文件的文件名稱

+0

[如何閱讀目錄中的所有文件](http://stackoverflow.com/questions/3950682/php-how-to-read-only-txt-files-in-a-directory)&[如何打印單個文件的內容] (http://php.net/manual/en/function.file-get-contents.php) - 沒有要回答的問題,這一切都已經回答過了。你只需要看。 –

+1

你的問題並不清楚 - 如果你只有一個文件列表中的一個項目總是被排序,而且你保存了文件,那麼你最好知道它的名字...... –

回答

1

此代碼

<?php 
if ($directory = opendir('/')) { // if the dir can be opened 
    while (false !== ($filename = readdir($directory))) { 
     echo "$filename\n"; //printing file names 
    } 
    closedir($directory); 
} 
?> 

(從http://php.net/manual/en/function.readdir.php

會顯示文件的無序列表。要顯示它的順序。你可以把它放在一個數組中,然後對其進行排序。

但是,如果你有一個包含文件名的文本文件裏,使用file_get_contentshttp://php.net/manual/en/function.file-get-contents.php),把它放在數組,然後整理了這一點。

+0

謝謝你。 :) – freeloader

2

你試過類似file_get_contents的功能嗎? 這聽起來像你想要做這樣的事情http://php.net/manual/en/function.file-get-contents.php 如果你想顯示文件的內容,你會再調用是這樣的:

$file = file_get_contents('/people.txt', FILE_USE_INCLUDE_PATH); 
+0

感謝您的所有例子! :)(y) – freeloader

1
<?php 
$txtFiles = glob("*.txt") 
?> 

<html> 
<ul> 
    <?php foreach ($txtFiles as $fileName) { ?> 
    <li><?php echo $filename ?></li> 
    <?php } ?> 
</ul> 
</html> 

檢查http://php.net/glob

+0

感謝您的答覆,我會試試這個。 :) – freeloader

相關問題