用于生成文件夹链接的php脚本

时间:2013-07-20 01:50:40

标签: php

需要帮助php脚本/页面生成文件夹链接。 有一个主页,上面有我使用Lightroom上传的照片 - 每个相册都在一个单独的文件夹中。

结构是:

mysite.com  
  |--images  
    |--folder1  
    |--folder2  
    |--folder3  
    .  
    . 

所以我想得到一个动态的index.php文件,该文件生成指向“images”的所有子文件夹的链接,而不是我在mysite.com的根目录中获得的静态index.html文件:

<html>
  <body>
    <a href="mysite.com/images/folder1" target="_blank">folder1</a>
    <a href="mysite.com/images/folder2" target="_blank">folder2</a>
    <a href="mysite.com/images/folder3" target="_blank">folder3</a>
    .
    .
  </body>
</html>

提前完成

3 个答案:

答案 0 :(得分:1)

<?php
    $files = scandir();
    $dirs = array(); // contains all your images folder
    foreach ($files as $file) {
        if (is_dir($file)) {
           $dirs[] = $file;
        }
    }
?>

使用dirs数组动态生成链接

答案 1 :(得分:0)

尝试这样的事情:

$contents = glob('mysite.com/images/*');
foreach ($contents as content) {
    $path = explode('/', $content);
    $folder = array_pop($path);
    echo '<a href="' . $content . '" target="_blank">' . $folder . '</a>';
}

或者这个:

if ($handle = opendir('mysite.com/images/') {
    while (false !== ($content = readdir($handle))) {
        echo echo '<a href="mysite.com/images/' . $content . '" target="_blank">' . $content . '</a>';
    }
    closedir($handle);
}

答案 2 :(得分:0)

也许是这样的:

$dir = "mysite.com/images/";
$dh = opendir($dir);
while ($f = readdir($dh)) {
  $fullpath = $dir."/".$f;
  if ($f{0} == "." || !is_dir($fullpath)) continue;
  echo "<a href=\"$fullpath\" target=\"_blank\">$f</a>\n";
}
closedir($dh);

当我需要所有内容(即something/*)时,我更喜欢readdir()而不是glob()因为speed而且内存消耗更少(按文件读取目录文件,而不是将整个事物放入数组中。)

如果我没有弄错的话,glob()确实会省略.*个文件而不需要$fullpath变量,所以如果你的速度很快,你可能想要做一些测试