【问题标题】:PHP dynamic gallery problemPHP动态图库问题
【发布时间】:2011-02-13 00:35:01
【问题描述】:

我正在尝试创建一个动态图片库脚本,以便客户端在我完成网站后,可以通过 FTP 上传图片并且网站会自动更新。我正在使用我在这里找到的脚本,但我无法让它为我的生活工作。 PHP 写入信息,但永远不会真正写出它应该在目录中找到的图像。不知道为什么。演示here,你可以看到工作代码(没有PHP)here

<?php 
function getDirTree($dir,$p=true) {
    $d = dir($dir);$x=array();
    while (false !== ($r = $d->read())) {
        if($r!="."&&$r!=".."&&(($p==false&&is_dir($dir.$r))||$p==true)) {
                $x[$r] = (is_dir($dir.$r)?array():(is_file($dir.$r)?true:false));
        }
    }

    foreach ($x as $key => $value) {
        if (is_dir($dir.$key."/")) {
                $x[$key] = getDirTree($dir.$key."/",$p);
        }
    }

    ksort($x);
    return $x;
}
$path = "../images/bettydew/";
$tree = getDirTree($path);

echo '<ul class="gallery">';

foreach($tree as $element => $eval) {
    if (is_array($eval)) {
        foreach($eval as $file => $value) {
                if (strstr($file, "png")||strstr($file, "jpg")||strstr($file, "bmp")||strstr($file, "gif")) {
                        $item = $path.$file;
                        echo '<a href="javascript:void(0);"><img src="'.$item.'" alt="'.$item.'"/></a>';
                }
        }
    }
}

echo '</ul>';
?>

【问题讨论】:

    标签: php dynamic gallery


    【解决方案1】:

    子目录中的路径问题和递归问题。

    也许试试这个:

    <?php
    $path = "./images/bettydew/";
    $file_array = array ();
    readThisDir ( $path, &$file_array );
    
    echo '<ul class="gallery">';
    foreach ( $file_array as $file )
    {
      if (strstr($file, "png")||strstr($file, "jpg")||strstr($file, "bmp")||strstr($file, "gif"))
      {
      list($width, $height) = getimagesize($file);
      echo '<li><a href="javascript:void(0);"><img src="'.$file.'" width="'.$width.'" height="'.$height.'" alt="'.$file.'"/></a></li>';
      }
    }
    echo '</ul>';
    
      function readThisDir ( $path, $arr )
      {
        if ($handle = opendir($path)) 
        {
            while (false !== ($file = readdir($handle))) 
            {
                if ($file != "." && $file != "..") 
                {
                  if (is_dir ( $path."/".$file ))
                  {
                    readThisDir ($path."/".$file, &$arr);
                  } else {
                    $arr[] = $path."/".$file;
                  }  
                }
            }
            closedir($handle);
        }
      }
    ?>
    

    【讨论】:

    【解决方案2】:

    要么你的路径是错误的......当我向 $path 变量提供错误的路径时,它导致了类似的错误。

    或者您没有对该目录的读取权限...也要检查权限

    记得检查路径末尾的“/”,因为没有它,您的代码无法在子目录中进行递归搜索...

    最后你的代码在写输出时不会给出子目录文件的正确文件路径...所以也要检查一下...

    【讨论】:

    • 一切似乎都井然有序。我不知道为什么它不起作用。一切都是可读的,最后有一个/。我想要的只是一个简单的文件来扫描文件夹中的图像,并将它们添加到一个无组织的列表中。
    • 您的文件是否托管在服务器中?你有读权限吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    • 2013-09-23
    • 1970-01-01
    • 2014-04-18
    相关资源
    最近更新 更多