所以我有一个网站,我正在制作一个课程,我遇到了一个问题。
我试图将.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来托管这些文件。我也使用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++;
}
}