Search code examples
phphtml5-videomediareaddir

How to Identify and send a unique id for sub folders


I don't know how to start but what I am trying to do is to scan a directory which contains video files and send the ids to another php called video.php to display the video. However when there is a subfolder I want to identify it and send its unique id to video.php to perform different action. I don't know how to identify the subfolders.

main.php

$dir = "./media/";
if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $list[] = $file;
        }
    }
    closedir($handle);
}
krsort($list);
foreach($list as $file) {
    $thelist .= '<div class="button"><div class="item">
        <a href="video.php?id='.$file.'">
        <img src="play.png" 
        alt="VIDEO   Thumbnail"style="width:250;height:150;border:0;">
        <span class="caption">'.$file.'</span> </a></div></div>';
}

video.php

<?php
$id = $_GET['id']; 
?>

<video class='center' controls autoplay>
    <source src="./media/<?php echo $id; ?>" type="video/mp4">
    <source src="./media/<?php echo $id; ?>" type="video/ogg">
    Your browser does not support the video tag.
</video>

Solution

  • I am not exactly sure what you are asking, but I'll take a stab anyhow. Please offer specific feedback if I am not providing a suitable solution.

    main.php

    $dir="./media/";
    if($handle=opendir($dir)){
        while(false!==($file=readdir($handle))){
            if($file!="." && $file!=".."){
                if(is_dir("$dir/$file")){   // <-- I think this is the part you are looking for
                    $dirs[]=$file;
                }else{
                    $files[]=$file;
                }
            }
        }
        closedir($handle);
    }
    
    krsort($files);  // not sure why you want to sort DESC
    foreach($files as $file){
        $thelist.="<div class=\"button\"><div class=\"item\"><a href=\"video.php?id=$file\"><img src=\"play.png\" alt=\"VIDEO Thumbnail\" style=\"width:250;height:150;border:0;\"><span class=\"caption\">$file</span>/a></div></div>";
    }
    
    krsort($dirs);  // not sure why you want to sort DESC
    foreach($dirs as $dir){
        // provide links to video.php with directory id $dir
    }
    

    video.php

    $fileid=$_GET['fileid'];  // sanitize this
    $dirid=$_GET['dirid'];   // sanitize this
    
    if($fileid){
        echo "<video class=\"center\" controls autoplay>";
            echo "<source src=\"./media/$fileid\" type=\"video/mp4\">";
            echo "<source src=\"./media/$fileid\" type=\"video/ogg\">";
            echo "Your browser does not support the video tag.";
        echo "</video>";
    }elseif($dirid){
        // do what you like with $dirid
    }else{
        // default action when no id is provided
    }