如果所有图像存在于文件夹php中,如何显示它们



我有一个php脚本,该脚本扫描指定的电影目录,然后使用for loop和php在网页上显示其样式。代码在下面。我尝试使用地球,但是一旦我在数组中拥有所有图像,如何将它们与所有电影进行比较,然后如果图像和电影文件夹匹配了正确的名称?

<?php
// set dir to the directiry you want to index
$dir = 'Movies/';
// scanning the directory you set
$scan = scandir($dir);
// I have this value at 11 because the first movie shows up
// in the 11th file in the array
$file = 11;
// This then removes the first useless files from our array
$scanned = array_slice($scan, $file);
// gets the amount of files
$FileNum = count($scanned);
// display all images and fanart
$images = glob('*.jpg');
// for loop that goes through the array
    for ($i = 0; $i <= $FileNum; $i++) {
      // gives the class for styling
    echo  '<li class="image">';

这是问题

    // check if there is fanart/images in the folders
    if (file_exists($dir . $scanned[$i] . $images)) {
      // if there is images display them styled
      echo '<img id="box1" src="' . $dir . $scanned[$i] . '*.jpg' . '" width="280" height="150" />';
    } else {
      // if not then display default image
    echo '<img id="box1" src="http://placehold.it/280x150" width="280" height="150" />';
  }
            // make the box clickable to where the folder is located
            echo '<a href="'. $dir . $scanned[$i] .'">';
            // display the name of the movie and some JS
             echo '<span class="text-content"><span>' . $scanned[$i] .'<br><br><i class="fa fa-4x  fa-play-circle-o"></i><br><br><i class="fa fa-chevron-down" onclick="openNav()" aria-hidden="true"></i></span></span> </a>';
           }

文件结构如下

      `MOVIES---
                --random movies
                   --mp4 and .jpg files`

澄清我的问题是 - 有没有办法检查文件是否存在,并且是否确实将其放入数组中?我已经尝试使用Glob,但是无法检查文件是否存在。

好吧,您的*中有一个CC_1,我认为它不需要那里。

以及您的电影目录被更新或图像。那么您的脚本不再起作用。因为硬切片(第11个文件)。

也许这对您有用:

<?php
// movies
$dir = "movies/";
$files = scandir($dir);
$movies = array();
$images = array();
foreach ($files as $file) {
    // check for the mime type:
    $mime = mime_content_type($dir . $file);
    $type = substr($mime, 0,5);
    $filename = pathinfo($dir . $file, PATHINFO_FILENAME);
    if ($type == "video") $movies[] = $file;
    if ($type == "image") $images[] = $filename;
}
foreach ($movies as $movie) {
    $placeholder = true;
    foreach($images as $image) {
        if (strpos($movie, $image) !== false) {
            $placeholder = false;
            continue;
        }
    }
    if ($placeholder) {
        echo $movie . " - placeholder<br>";
    } else {
        echo $movie . " - image<br>";
    }
}

它与哑剧类型一起使用。

最新更新