使用php读取目录中的目录和文件

时间:2017-07-14 15:08:57

标签: php file

我有一个用于读取该文件夹中的文件夹和文件的代码。 基本上,这个代码会做什么,它将显示所有文件。并为他们创建链接。当我点击链接文件将在浏览器中打开(如果支持,如图像)或下载。 但问题是它显示文件但不打开它们。 请问有什么身体突出问题吗? 这是完整的代码。

<?php
$msgz = "";

if($handle = opendir('.'))
{
 while (false !== ($filez = readdir($handle)))
 {
 if (($filez != ".") && ($filez != ".."))
 {
 $msgz .= '<li><a href="'.$filez.'">'.$filez.'</a></li>';

 }

 }
 closedir($handle);
}

?>
<!DOCTYPE html>
<html>
<head>
<title>List files and directories inside the specified path in PHP</title>
</head>
<body>
<h2>List files and directories inside the specified path in PHP</h2>
<p>List of files:</p>
<ul>
<p><?php echo $msgz ?></p>
</ul>
</body>
</html>

这是浏览器的屏幕截图。

image showing that link of the desired file is present in address bar but file is not open

1 个答案:

答案 0 :(得分:0)

将您的文件命名为index.php它将起作用。

相关问题