【问题标题】:How to display all images if they exist in a folder PHP?如果它们存在于文件夹 PHP 中,如何显示所有图像?
【发布时间】:2017-12-18 18:57:55
【问题描述】:

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

【问题讨论】:

  • 您能以某种方式将其缩小为更具体、更明确的问题吗?就目前而言,它感觉更像是一个“你能为我编写这个期望的行为吗?” 之类的问题,但对于未来的读者来说绝对不是很有用。查看How to Ask 了解有关如何改进您的问题的一些信息。
  • 我改变了问题。为了澄清,我要问的是 - 有没有办法检查文件是否存在,如果存在,将其放入数组中?我尝试使用 glob,但这并不能检查文件是否存在。

标签: php arrays image indexing movie


【解决方案1】:

你的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

【讨论】:

  • 问题是我没有图像文件夹。下载电影后,它们会被转储到一个文件夹中 - 所有 mp4/mkv 文件和所有 jpg 文件。那么,牢记这一点,我将如何实施呢?
  • HeroBrian,只要稍加努力,您应该能够相应地调整给定的代码。
猜你喜欢
  • 2018-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-07
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多