简体   繁体   English

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

[英]simplest home streaming server php alphabetization

this is the code im using: 这是我使用的代码:

<?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);

              }
        };
        ?>

im attempting to host a tiny simple http server with php on my router so i can stream videos to my phone while i work 即时通讯试图在路由器上使用php托管一个小型的简单http服务器,以便我可以在工作时将视频流式传输到手机

basically, i have a index.php with in every folder that has videos 基本上,我在每个有视频的文件夹中都有一个index.php

this works fine, when the video is encoded right, but when it lists the videos in the folder theyre not "alphabetized" or non-sequential 如果视频编码正确,但是当它在文件夹中列出视频时,它们没有“按字母顺序排列”或非顺序排列,则效果很好

they come up like this: 他们这样来:

Watch "House Of Cards S01E01.mp4"
Watch "House Of Cards S01E08.mp4"
Watch "House Of Cards S01E05.mp4"
Watch "House Of Cards S01E11.mp4"
Watch "House Of Cards S01E03.mp4"
Watch "House Of Cards S01E10.mp4"
Watch "House Of Cards S01E02.mp4"
Watch "House Of Cards S01E07.mp4"
Watch "House Of Cards S01E09.mp4"
Watch "House Of Cards S01E13.mp4"
Watch "House Of Cards S01E12.mp4"
Watch "House Of Cards S01E04.mp4"
Watch "House Of Cards S01E06.mp4"

anyone know how i can make this code turn up asequential or "alphabetized" list? 有谁知道我如何使此代码出现不连续或“按字母顺序排列”的列表?

You should take a look here: natsort 您应该在这里看看: natsort

Essentially this function will use an algorithm to sort your files in a "natural" order, instead of alphabetical as it is now. 本质上,此函数将使用算法以“自然”顺序对文件进行排序,而不是像现在那样按字母顺序排序。

To add onto your code, you could add all your files into an array when looping through files in the directory, and at the end, run natsort, before printing links for each of them. 要添加到您的代码中,可以在遍历目录中的文件时将所有文件添加到数组中,最后运行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>";
    }

Hope this helps! 希望这可以帮助!

        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>
                        ";

}

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

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