簡體   English   中英

用PHP scandir()從數組創建有序列表

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

scandir()上閱讀時,出現問題,無法顯示任何內容。

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

我以為我沒有對目錄和scandir()做其他事情,我之前已經做過,並且工作正常。

目錄權限會引起問題嗎?

**請注意,您不能使用URL,您必須有權訪問文件夾/文件

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():

$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