Search code examples
phphtmlfilesystemshref

How to create file paths to use in html inside .php loop


I have a simple file base on my webpage, let's say ProjectFolder is the main folder, which contains completely all files.

The folder I want to access goes like this:

ProjectFolder/Movies/Movie1/cover/Movie1.jpg  
ProjectFolder/Movies/Movie1/movie/Movie1.mp4  

ProjectFolder/Movies/Movie2/cover/Movie2.jpg  
ProjectFolder/Movies/Movie2/movie/Movie2.mp4  

ProjectFolder/Movies/Movie3/cover/Movie3.jpg  
ProjectFolder/Movies/Movie3/movie/Movie3.mp4  

And so on... Movie 1,2,3,4... etc folders can be uploaded manually, so I have to be able to show all of them, no matter the count (they are also with various names, Movie1,2,3 is just for example)

Using php, I made a loop that goes through all subfolders of the ProjectFolder/Movie folder and for each one of them, creates a portion of html code, in which I need to use:

  1. Link to a video file, currently "Movies/Movie1/movie/Movie1.mp4"
  2. Link to a video file, currently "Movies/Movie1/cover/Movie1.jpg"
  3. Button, which goes to a page: "http://myproject.com/am/Movie1.php"

This is my php:

$i = 1;  
$path = "./Movies";  
$dir = new DirectoryIterator($path);  
foreach ($dir as $fileinfo)  
{  
   if ($fileinfo->isDir() && !$fileinfo->isDot())  
   {  
     //(html code)  
   }  
} 

What php code should I replace the html "" parts from 1, 2 and 3?


Solution

  • <?php
    
    $dir = './Movies';
    $directories = array();
    $files_list  = array();
    $files = scandir($dir);
    
    foreach ($files as $file)
    {
        if (($file != '.') && ($file != '..'))
        {
             if (is_dir($dir.'/'.$file))
             {
                  $directories[]  = $file;
             }
        }
    }
    
    foreach ($directories as $directory)
    { 
    
    ?>
    

    HTML code....

    <?php echo "Movies/"; echo $directory; echo "/movie/"; echo $directory; echo ".mp4"; ?>
    

    Same for ".jpg" and the button code looks like this:

    <div>
       <form action="<?php echo "http://blabla.com/"; echo $directory; echo ".php"; ?>">
            <input type="submit" class="button" value="GO TO <?php echo $directory; ?>">
       </form>
    </div>