2017-12-18 14 views
-1

나는 지정된 영화 디렉토리를 스캔하고 for 루프와 PHP를 사용하여 웹 페이지에 스타일을 표시하는 PHP 스크립트를 가지고있다. 코드는 다음과 같습니다. glob을 사용하여 시도했지만 배열에 모든 이미지가 있으면 모든 이미지가 포함 된 배열과 이미지를 비교 한 다음 이미지와 동영상 폴더가 일치하는 이름으로 이미지를 표시합니까? 폴더에 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를 사용하여 시도했지만 파일이 있는지 확인할 수 없습니다.

+0

어떻게하면 구체적이고 명확한 질문으로 좁힐 수 있습니까? 마찬가지로, "당신이 원하는 행동을 저를 위해 코딩 할 수 있습니까?"라는 질문이 더 많지만, 미래의 독자에게는 그다지 유용하지는 않습니다. 질문을 개선하는 방법에 대한 정보는 [ask]를 확인하십시오. – domsson

+0

나는 질문을 변경했다. 명확히하기 위해 내가 묻는 것은 - 파일이 있는지 확인하는 방법이 있습니까? 그렇다면 파일을 배열에 넣으십시오. glob를 사용하여 시도했지만 파일이 존재하는지 확인하지 않습니다. – HeroBrian

답변

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와 함께 작동합니다.

+0

문제는 이미지 폴더가 없다는 것입니다. 영화가 다운로드되면 단일 폴더에 버려집니다 - 모든 mp4/mkv 파일과 모든 jpg 파일. 그렇다면 이것을 어떻게 염두에두고 구현할 것인가? – HeroBrian

+0

HeroBrian, 약간의 노력으로 주어진 코드를 적절하게 변형 할 수 있어야합니다. – domsson

+0

나는 당신을 위해 코드를 조정했다 @HeroBrian – Wouter075