【发布时间】: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