【问题标题】:Listing directory on a server using php使用 php 在服务器上列出目录
【发布时间】:2016-07-16 09:14:02
【问题描述】:

我需要使用服务器上的 php 脚本在服务器上列出一组目录。这是我在服务器上的文件夹结构:(ftp)

这是写在 listDirectory.php 中的代码(我需要知道 /public_html/stickers/ 的子​​目录是什么以及每个子目录中存在多少文件)我知道我可以- 对其进行编码,但我需要一种动态方法,因为文件会发生很大变化。

<html>
<title>Directories</title>
<body>
    <?php
        $directory = $_SERVER['DOCUMENT_ROOT'] . '/stickers/';
        echo "<p>$directory</p>";
        
        $sticker_directories =  array(scandir($directory));
        
        echo "<p>Number of subdirs at $directory :". count($sticker_directorie) . "</p>";
        
        foreach ($sticker_directories as $dir){
            $working_dir = $directory.$dir;
            echo "<p>$working_dir</p>"; 
            if (is_dir($working_dir)){
                
                echo "<p>$working_dir" . count(scandir($working_dir)) . "</p>";
            }
        }   
    ?>
</body>
</html> 

这是输出:

/home/u826063014/public_html/stickers/

Number of subdirs at /home/u826063014/public_html/stickers/ :0

/home/u826063014/public_html/stickers/Array

这不是我所期待的结果。我做错了什么?

(我对这一切完全陌生)

【问题讨论】:

    标签: php server directory cpanel


    【解决方案1】:

    首先scandir 已经返回数组。无需使用array

    $sticker_directories = scandir($directory);
    
    // `scandir` returns both files and subdirs. So, this string is not true
    echo "<p>Number of subdirs at $directory :". count($sticker_directories) . "</p>";
    //                                                                    ^ s missed
    

    之后一切都会好起来的。

    【讨论】:

    • 谢谢我应该早点问它花一整天的时间......它现在正在工作。当 StackOverflow 允许我时,我会将您的回复标记为答案。奇怪的是这段代码在 Windows 上运行,但在服务器上没有运行.. 再次感谢。
    猜你喜欢
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多