简体   繁体   English

用PHP scandir()从数组创建有序列表

[英]scandir() with php to create ordered list from array

Reading up on scandir() , having an issue getting anything to display. scandir()上阅读时,出现问题,无法显示任何内容。

<?php
    $dir = 'http://www.universaldynamicmedia.com/sandbox/Images';
    $array = scandir($dir);
    foreach($array as $key => $value)
        {
            echo '<li><img src="' . $dir . $value . '" /></li>';
        }
?>

I supposed I'm not doing something right with the directory and scandir() as the other stuff, I've done before and it works fine. 我以为我没有对目录和scandir()做其他事情,我之前已经做过,并且工作正常。

Would directory permissions cause an issue? 目录权限会引起问题吗?

** Note you cannot use a URL, you must have access to the folders/files **请注意,您不能使用URL,您必须有权访问文件夹/文件

scandir(): SCANDIR():

$dir = "PATH_TO_DIRECTORY";
$exclude = array( ".","..","error_log","_notes" );
if (is_dir($dir)) {
    $files = scandir($dir);
    foreach($files as $file){
        if(!in_array($file,$exclude)){
            echo '<li><img src="' . $dir . $file . '" /></li>';
        }
    }
}

readdir(): READDIR():

$dir = "PATH_TO_DIRECTORY";
$exclude = array( ".","..","error_log","_notes" );
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if(!in_array($file,$exclude))
                 echo '<li><img src="' . $dir . $file . '" /></li>';
        }
        closedir($dh);
    }
}?>

scandir()仅适用于本地文件系统,您需要将其更改为:

$dir = '/path/to/document/root/sandbox/Images';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM