PHP的循环不读文件

时间:2019-02-27 20:31:31

标签: php mysql for-loop directory wamp

因此,我有一个正在制作课程的网站,但遇到了问题。

我正在尝试将.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函数未找到文件,并取消了操作。

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

<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托管这些文件。我也在使用记事本++编辑PHP和.txt文件。

1 个答案:

答案 0 :(得分:2)

如果文本文件与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++;
    }
}