1

I'm using WAMP for my projects, but I'm not satisfied with the original index.php. I want to display the projects folder on the WAMP index page. Here's what I've managed to think about:

<? 
$sisis = file_get_contents('projektit');
echo $sisis;
?>

But this won't do anything actually. Here's the projects folder displayed in browser:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
    <head>
        <title>Index of /projektit</title>
    </head>
    <body>
        <h1>Index of /projektit</h1>
        <table>
            <tr>
                <th>
                    <img src="/icons/blank.gif" alt="[ICO]">
                </th>
                <th>
                    <a href="?C=N;O=D">Name</a>
                </th>
                <th>
                    <a href="?C=M;O=A">Last modified</a>
                </th>
                <th>
                    <a href="?C=S;O=A">Size</a>
                </th>
                <th>
                    <a href="?C=D;O=A">Description</a>
                </th>
            </tr>
            <tr>
                <th colspan="5">
                    <hr>
                </th>
            </tr>
            <tr>
                <td valign="top">
                    <img src="/icons/back.gif" alt="[DIR]">
                </td>
                <td>
                    <a href="/">Parent Directory</a>
                </td>
                <td>&nbsp;</td>
                <td align="right">-</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <td valign="top">
                    <img src="/icons/folder.gif" alt="[DIR]">
                </td>
                <td>
                    <a href="CCCKauppa/">CCCKauppa/</a>
                </td>
                <td align="right">24-Aug-2012 22:43</td>
                <td align="right">-</td>
                <td>&nbsp;</td>
            </tr>
            <tr>
                <th colspan="5">
                    <hr>
                </th>
            </tr>
        </table>
    </body>

</html>

How would I display the links only on this page?

4

2 回答 2

1

据我了解您的问题,您想生成带有链接的目录列表。(如果我理解错了,请评论回答)

对于 PHP 5 及更高版本

你可以使用scandir(PHP5函数)并做类似的事情

<ul> 
<?php 
$dir = '/projektit'; 
$files = scandir($dir); 
foreach($files as $ind_file){ 
?> 
<li><a href="<?php echo $dir."/".$ind_file;?>"><?php echo $ind_file;?></a></li> 
<?php 
} 
?> 
</ul> 

我还没有测试它,所以可能需要一些调整。

对于较旧的 PHP 版本

如果这不起作用,您需要opendirreaddirclosedir函数。

$dir = opendir('projektit/'); 
echo '<ul>';
while ($read = readdir($dir)) 
{

if ($read!='.' && $read!='..') 
{ 
echo '<li><a href="files/'.$read.'">'.$read.'</a></li>'; 
}

}

echo '</ul>';

closedir($dir); 

更新

此处已准备好使用具有返回父文件夹功能的目录列表器。演示

于 2012-08-25T09:16:47.757 回答
1

您可以使用 scandir 获取文件夹中的所有文件。

$scanned = scandir($your_path);

// if you want to display only folders then you can:
// But please notice that array filter with anonymous function is available since PHP 5.3.0
$scanned = array_filter($scanned, function($el) { if (strpos($el, '.') === false) return $el; });

foreach($scanned as $folder)
   echo $folder;

基本上,这个想法是获取一个文件夹中的所有文件($your_path在这种情况下),并删除这些带有扩展名的文件。因此,您手头只有文件夹。

于 2012-08-25T09:19:39.530 回答