我在WordPress中製作自定義主題。現在在主題文件夾內是另一個名爲「php」的文件夾,裏面有一個文本文件,比方說「names.txt」。現在我想要做的是從「php」文件夾中讀取文本文件。我在我的index.php這個代碼:閱讀文本文件的無限循環
<?php
$file = fopen("/php/names.txt","r");
while(! feof($file))
{
echo fgets($file). "<br />";
}
fclose($file);
?>
但我的網頁被卡在無限循環,有錯誤,該文件不存在,但它確實。非常需要幫助。 更新:我試着在一個單獨的php.file中運行上面的代碼,我將它放在與「names.txt」文件相同的目錄中,並讀取數據。
UPDATE [解決]:
<?php
$location = get_template_directory() . "/php/admin.txt";
if (file_exists($location)) {
$file = fopen($location, "r");
while(!feof($file)) {
echo fgets($file). "<br />";
}
fclose($file);
}
else
{echo "no file.";}
?>
功效神奇,感謝@MackieeE
嘗試添加一個空行到你的「names.txt中」文件 –
數據末尾的「names.txt中內'檔案?或者像這樣: $ file = fopen(「/ php/names.txt _」,「r」); –
打開你的names.txt文件並在其底部/底部添加一個空行。 –