PHP For循环不读取文件

PHP For循环不读取文件,php,mysql,for-loop,directory,wamp,Php,Mysql,For Loop,Directory,Wamp,所以我有一个网站,我正在制作一个课程,我遇到了一个问题 我试图将.txt文件的内容读入for循环,以便将内容打印到表单选项选择中 以下是循环的: <p> City Where You Want to Reside:<br /> <select name="city" size="1"> <option value="-">-</option> <?php $DOCUMENT_ROOT = $_SERVER['DO

所以我有一个网站,我正在制作一个课程,我遇到了一个问题

我试图将.txt文件的内容读入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>
除了“-”之外,我没有从选项中得到任何回报。我认为问题可能是它找不到文件,因为表单只输出“-”选项,而file_exists函数没有找到文件,因此取消了操作

当我“查看页面源代码”时,它指出城市选择的代码应该是:

<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>

(!)警告:文件(C:/wamp64/www\datacities.txt):无法打开流:第50行的C:\wamp64\www\assignment_3.php中没有此类文件或目录
调用堆栈
#时间记忆功能定位
10.0101408544{main}()…\assignment_3.php:0
20.0101408600
()…\assignment_3.php:50

(!)警告:count():参数必须是在第50行的C:\wamp64\www\assignment_3.php中实现可数的数组或对象 调用堆栈 #时间记忆功能定位 10.0101408544{main}()…\assignment_3.php:0
(!)警告:fopen(C:/wamp64/www\datacities.txt):无法打开流:第54行的C:\wamp64\www\assignment_3.php中没有此类文件或目录 调用堆栈 #时间记忆功能定位 10.0101408544{main}()…\assignment_3.php:0 20.0118409328 ()…\assignment_3.php:54
我确实注意到
警告:文件(C:/wamp64/wwww\datacities.txt):无法打开流:在第50行的C:\wamp64\www\assignment_3.php中没有这样的文件或目录
,并想知道是否是WW导致了问题?如果是,我如何修复它


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

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

$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>';
}

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

$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>';
}

是的,有一个额外的
www
来自某个地方,但从代码中看不到。是的,有一个额外的
www
来自某个地方,但从代码中看不到。向上投票,因为OP不断的变量声明令人愤怒。Doh!我只是把它拆开以摆脱滚动条:-(MonkeyZeus,对于变量声明我很抱歉,我怎样才能使它们更好?我愿意回去编辑我的问题。另外,我没有写这段代码,实际上是讲师的代码也被破坏了,这可能表明我有一个错误的文件夹层次结构。此外,cities.txt文件位于一个名为“data”的文件夹中。)在C:\wamp64\www\data下,cities.txt文件与PHP文件不在同一级别。PHP文件位于C:\wamp64\www中。编辑:变量声明得太多,因为在下一节中,他会检查函数。这减少了变量。Yahoo!第二部分修复了它!我只需将数据\添加到cities.txt中即可获得快速提问,为什么你认为讲师的文档根变量中有所有令人困惑的“'data.'cities.txt”?”如果这同样有效的话,我很抱歉由于缺少格式或其他原因给您带来的不便。升级投票,因为OP不断的变量声明令人愤怒。Doh!我只是将其拆分以摆脱滚动条:-(MonkeyZeus,对于变量声明我很抱歉,我怎样才能使它们更好?我愿意回去编辑我的问题。另外,我没有写这段代码,实际上是讲师的代码也被破坏了,这可能表明我有一个错误的文件夹层次结构。此外,cities.txt文件位于一个名为“data”的文件夹中。)在C:\wamp64\www\data下,cities.txt文件与PHP文件不在同一级别。PHP文件位于C:\wamp64\www中。编辑:变量声明得太多,因为在下一节中,他会检查函数。这减少了变量。Yahoo!第二部分修复了它!我只需将数据\添加到cities.txt中即可获得很高兴找到正确的文件。快速提问,为什么你认为讲师的文档根变量中有所有令人困惑的“'data.'cities.txt”之类的东西?如果这同样有效的话?我很抱歉由于缺少格式或其他原因给您带来的不便。
$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++;
    }
}