PHP动态图库问题

发布于 2024-10-17 02:00:47 字数 1399 浏览 2 评论 0原文

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

<?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>';
?>

I'm trying to create a dynamic image gallery script, so that a client, once I'm done with the site, can upload images via FTP and the site will update automatically. I'm using a script I found on here, but I cannot get it to work for the life of me. The PHP writes information, but will never actually write out the images it's supposed to find in the directory. Not sure why. Demo here and you can see the working code (without 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>';
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟花肆意 2024-10-24 02:00:47

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

也许试试这个:

<?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);
    }
  }
?>

path problem and a recursion problem in the sub-directories.

perhaps try this:

<?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);
    }
  }
?>
过期以后 2024-10-24 02:00:47

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

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

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

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

Either Your path is mistake... it caused me similar error when i feed wrong path to the $path variable.

Or you do not have read permission on that directory... check the permissions too

And remember to check '/' at the end of your path as without it your code cannot do recursive search inside the child directory...

And finally your code will not give the correct file path of the files of sub-directory while writing output... so check it too...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文