无法在php中显示文件内容
我无法在同一网页中显示文本文件的内容。下面的代码称为display.php文件无法在php中显示文件内容,php,html,ajax,Php,Html,Ajax,我无法在同一网页中显示文本文件的内容。下面的代码称为display.php文件 <html> <head> <link rel="icon" type="image/png" href="logo.png"> <body> <?php $selectedfile = @$_POST['file_to_be_displayed']; $content = file($selectedfile); $data = impl
<html>
<head>
<link rel="icon" type="image/png" href="logo.png">
<body>
<?php
$selectedfile = @$_POST['file_to_be_displayed'];
$content = file($selectedfile);
$data = implode("<br>",$content);
echo $data;
?>
<?php
$dirname = "<directory to be scanned for files>";
$dir = opendir($dirname);
echo '<form name="displayfile" action="output.php" method="POST">';
echo '<select name="file_to_be_displayed">';
echo '<option value="">Select File</option>';
while(false != ($file = readdir($dir)))
{
if((($file != ".") and ($file != "..") and (($file != "store_show_commands_switches.tcl" and $file != "switches.txt"))and (($file != "store_show_commands_routers.tcl" and $file != "routers.txt"))and (($file != "store_show_commands_firewalls.tcl" and $file != "firewalls.txt"))))
{
echo "<option value=".$file.">$file</option>";
}
}
echo '</select>';
echo '<input type="submit" value="display content of file" />';
echo '</form>';
?>
</body>
</html>
output.php
<html>
<body>
<?php
$myFile =$_POST['file_to_be_displayed'];
$file = file_get_contents ("$myFile");
Echo $file;
?>
</body>
</html>
php将扫描目录中的txt文件,并在下拉菜单中显示它们,旁边有一个按钮以显示所选文件。但是,一旦我点击按钮,我会得到一个没有结果的空白页面,它应该会显示txt文件的内容
有没有办法解决这个问题,或者——甚至更好——有没有办法在同一display.php中显示文件的内容?我认为可以用ajax来完成,但不确定如何完成
谢谢
Pantelis尝试echo$myFile;相反,我恐怕不是这样做的。当尝试$myFile=$selectedfile时,仍不在网页上显示任何内容;在您的output.php文件中,也没有工作。我可以通过以下代码echo$\u POST['file\u to\u be\u display']获得文件名;但是,文件名不完整。如何在output.php中指定$dirname?它只知道文件名,而不知道可以找到它的目录