PHP For 循环不读取文件

PHP For Loop Not Reading File

所以我有一个正在制作的网站和一门课程,但我 运行 遇到了一个问题。

我正在尝试将 .txt 文件的内容读入 for 循环以将内容打印到表单选项选择中。

这是 for 循环:

<p>
City Where You Want to Reside:<br />
<select name="city" size="1">
    <option value="-">-</option>

<?php
    $DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];

    $filename = $DOCUMENT_ROOT.'data'.'cities.txt';

    $lines_in_file = count(file($filename));

    //print "<br>lines: ".$lines_in_file;

    $fp = fopen($filename, 'r');   //opens the file for reading

    $bool = file_exists($filename);

    if ($bool == true)
    {
        for ($ii = 1; $ii <= $lines_in_file; $ii++)
        {
            $line = fgets($fp);
            $file_city = trim($line);

            print '<option value="'.$file_city.'">'.$ii.':'.$file_city.'</option>';
        }

        fclose($fp);
    }
?>

</select>

cities.txt 文件有这个:

Chicago
Detroit
Toronto
Los Angeles

除了“-”之外,我没有从其他选项中得到任何回报。我认为问题可能是它找不到文件,因为表单只是输出“-”选项,而file_exists函数没有找到文件,并取消操作。

当我 "view page source" 它说城市选择的代码应该是:

<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' 

cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: file(C:/wamp64/wwwwww\datacities.txt): failed to open stream: No such file or directory in C:\wamp64\www\assignment_3.php on line <i>50</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0101</td><td bgcolor='#eeeeec' align='right'>408544</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\assignment_3.php' bgcolor='#eeeeec'>...\assignment_3.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0101</td><td bgcolor='#eeeeec' align='right'>408600</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.file' target='_new'>file</a>
(  )</td><td title='C:\wamp64\www\assignment_3.php' bgcolor='#eeeeec'>...\assignment_3.php<b>:</b>50</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: count(): Parameter must be an array or an object that implements Countable in C:\wamp64\www\assignment_3.php on line <i>50</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0101</td><td bgcolor='#eeeeec' align='right'>408544</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\assignment_3.php' bgcolor='#eeeeec'>...\assignment_3.php<b>:</b>0</td></tr>
</table></font>
<br />
<font size='1'><table class='xdebug-error xe-warning' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Warning: fopen(C:/wamp64/wwwwww\datacities.txt): failed to open stream: No such file or directory in C:\wamp64\www\assignment_3.php on line <i>54</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0101</td><td bgcolor='#eeeeec' align='right'>408544</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\assignment_3.php' bgcolor='#eeeeec'>...\assignment_3.php<b>:</b>0</td></tr>
<tr><td bgcolor='#eeeeec' align='center'>2</td><td bgcolor='#eeeeec' align='center'>0.0118</td><td bgcolor='#eeeeec' align='right'>409328</td><td bgcolor='#eeeeec'><a href='http://www.php.net/function.fopen' target='_new'>fopen</a>
(  )</td><td title='C:\wamp64\www\assignment_3.php' bgcolor='#eeeeec'>...\assignment_3.php<b>:</b>54</td></tr>
</table></font>

我确实注意到了 Warning: file(C:/wamp64/wwwwww\datacities.txt): failed to open stream: No such file or directory in C:\wamp64\www\assignment_3.php on line <i>50</i> 并想知道 wwwwww 是否会导致此问题?如果是这样,我该如何解决?

顺便说一下,我正在使用 WAMP 来托管这些文件。我也在使用 Notepad++ 编辑 PHP 和 .txt 文件。

如果文本文件与 PHP 文件位于同一目录中,则不要使用路径。例如,如果文件位于 data 子目录中,请使用以下内容。所有代码都可以通过以下方式完成:

$lines = file('data/cities.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($lines as $ii => $file_city) {
    echo '<option value="'.$file_city.'">'.($ii+1).':'.$file_city.'</option>';
}

无论你怎么做,你都不想用file加载文件然后打开它并逐行阅读。只读每一行直到文件结束:

$ii = 1;
if($fp = fopen('data/cities.txt', 'r')) {
    while($line = fgets($fp)) {
        $file_city = trim($line);
        echo '<option value="'.$file_city.'">'.$ii.':'.$file_city.'</option>';
        $ii++;
    }
}