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

问题描述 投票:-1回答:1

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

<?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,但无法检查文件是否存在。

php arrays image indexing movie
1个回答
0
投票

那么你的*有一个echo我不认为它需要在那里。

如果您的电影目录更新,或图像。然后你的脚本不再起作用了。由于硬切片(第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>";
    }
}

它适用于mime-type

© www.soinside.com 2019 - 2024. All rights reserved.