PHP无法从URL目录列出文件

时间:2014-02-17 19:46:48

标签: php wordpress

我试图将目录中的css文件列表添加到Wordpress模板中。 我试图使下面的代码工作没有运气。 我做错了什么?

$path = parse_url(TC_BASE_URL . 'custom/css/', PHP_URL_PATH);
$dir = $_SERVER['DOCUMENT_ROOT'] . $path;
$dir = str_replace("/", "\\",$dir);
$dir = str_replace("\\\\", "\\",$dir);
if (is_dir($dir)) {
    if ($dh == opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
        }
        closedir($dh);
    } else {
        echo $dir . ' Is Not Open';
    }
} else {
    echo $dir . ' Is Not A Directory';
}

1 个答案:

答案 0 :(得分:1)

我做了以下测试:

<?php
    $ruta = "C:\\www\\wp-content\\themes\\custom\\custom\\css\\";

    if (is_dir($ruta)) {
        if ($dh = opendir($ruta)) {
            while (($file = readdir($dh)) !== false) {
                echo "file name: $file : file type: " . filetype($ruta . $file) . "</br>";
            }
            closedir($dh);
        }
    }

?> 

输出:

file name: . : file type: dir
file name: .. : file type: dir
file name: Nuevo Documento de Microsoft Word.docx : file type: file
file name: Nuevo KuaiZip ZIP Archive file.zip : file type: file