PHP动态图库问题
我正在尝试创建一个动态图像库脚本,以便客户端在我完成该网站后可以通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
路径问题和子目录中的递归问题。
也许试试这个:
path problem and a recursion problem in the sub-directories.
perhaps try this:
要么你的路径是错误的...当我向 $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...