最简单的家庭流媒体服务器php字母排序

最简单的家庭流媒体服务器php字母排序,php,echo,media-player,media,alphabetical-sort,Php,Echo,Media Player,Media,Alphabetical Sort,这是我使用的代码: <?php $dir = "House Of Cards/"; $videoW = 320; $videoH = 240; if (is_dir($dir)) { if ($dh = opendir($dir)){ while (($file = readdir($dh)) !== false){

这是我使用的代码:

<?php

        $dir = "House Of Cards/";
        $videoW = 320;
        $videoH = 240;

        if (is_dir($dir))
        {
            if ($dh = opendir($dir)){

                while (($file = readdir($dh)) !== false){

                    if($file != '.' && $file != '..'){

                        echo "
                        <div style='display: block'>
                        <a href= \"$dir/$file\">Watch \"$file\"</a>
                        </div>
                        ";


                    }

                }

                closedir($dh);

              }
        };
        ?>

有人知道我如何让这些代码按顺序或字母顺序出现吗?

你应该看看这里:

本质上,这个函数将使用一种算法来按“自然”顺序对文件进行排序,而不是像现在这样按字母顺序排序

要添加到代码中,可以在目录中的文件循环时将所有文件添加到数组中,最后,在打印每个文件的链接之前运行natsort

<?php

    $dir = "House Of Cards/";
    $videoW = 320;
    $videoH = 240;

    $files = []; // Initialize empty array

    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
                if($file != '.' && $file != '..') {
                    $files[] = $file; // add this file to array
                }
            }
            closedir($dh);
        }
    }

    natsort($files); // naturally sort files

    // make a link for each file...
    foreach ($files as $file) {
        echo "<div style='display: block'>
             <a href= \"$dir/$file\">Watch \"$file\"</a>
             </div>";
    }
if(is_dir($dir))
{
如果($dh=opendir($dir)){
while(($file=readdir($dh))!==false){
如果($file!='.&&$file!='..')){
$files[]=$file;
}
}
closedir($dh);
}
};
$arrat_order[]=natsort($files);
foreach($arrat_订单为$value){
回声“
";
}

这项工作非常有效。几乎没有修改。(如果我可以的话,还有一个额外的“}”id将这个答案投票给你哦,糟糕,删除了这个额外的“}”):(请注意,natsort()将返回布尔值,而不是数组。请在此处查看php文档:
<?php

    $dir = "House Of Cards/";
    $videoW = 320;
    $videoH = 240;

    $files = []; // Initialize empty array

    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
                if($file != '.' && $file != '..') {
                    $files[] = $file; // add this file to array
                }
            }
            closedir($dh);
        }
    }

    natsort($files); // naturally sort files

    // make a link for each file...
    foreach ($files as $file) {
        echo "<div style='display: block'>
             <a href= \"$dir/$file\">Watch \"$file\"</a>
             </div>";
    }
        if (is_dir($dir))
        {
            if ($dh = opendir($dir)){

                while (($file = readdir($dh)) !== false){

                    if($file != '.' && $file != '..'){
                            $files[] = $file;
                    }

                }

                closedir($dh);

              }
        };

    $arrat_order[] =    natsort($files);
    foreach($arrat_order as $value){
                   echo "
                        <div style='display: block'>
                        <a href= \"$dir/$value\">Watch \"$value\"</a>
                        </div>
                        ";

}