0

how do i select only files that contain *.jpg on the file name / extension and ignore anything else. i realized that my code is trying to get anything it can get on the folder, how do i filter it so it only pick up the jpg files

<?php
    $folder = "photobooth/photobooth/Michelle_Illona_Alexander/animated/"; //folder tempat gambar disimpan  
    $handle = opendir($folder); 
    $i = 1;
    while(false !== ($file = readdir($handle) )){  
    if($file != '.' && $file != '..'){
$file2=str_replace("_mp4.jpg","",$file);
$file3=substr($file,0);
//$filenames= directory(".","jpg");
//foreach ($filenames as $value)
//{
        echo '<li>'.
        '<a href="photobooth/photobooth/Michelle_Illona_Alexander/animated/'.$file2.'.mp4">
        <img src="photobooth/photobooth/Michelle_Illona_Alexander/animated/'.$file.'" width="300" title="" type="jpg"></a>'.
        '<br/></li>';  
    if(($i % 4) == 0){  
    echo '<br/>';
    echo '<br/>';
    echo '<br/>';
    echo '<br/>';
    echo '<br/>';
    echo '<br/>';
    echo '<br/>';
    echo '<br/>';
    echo '<br/>';
        }     
        $i++;
        if($i==0)
           break;
//}
 }    
}  
?>
idnawsi
  • 23
  • 7
  • 1
    Possible duplicate of [PHP check if file is an image](https://stackoverflow.com/questions/15408125/php-check-if-file-is-an-image) – BlackNetworkBit Sep 09 '18 at 12:15
  • i see, i think its kind of different, because i want it only take a look on image file in the folder and ignore any different kind of file for example *.mp4. and honestly i dont know how to put it on my code, im new to this – idnawsi Sep 09 '18 at 12:24

2 Answers2

0

You could add the check like :

if( strpos($filename, '.jpg') >= 0) {
 echo "this is a jpg image";
}

If you want only the extension with jpg, you can use a pregmatch with this regex /^.*\.(jpg)$/i

http://php.net/manual/it/function.preg-match.php

Alessandro Candon
  • 543
  • 2
  • 6
  • 21
0

It could be done like this :

<?php
$folder = "photobooth/photobooth/Michelle_Illona_Alexander/animated/"; //folder tempat gambar disimpan  
$handle = opendir($folder); 
$i = 1;
while(false !== ($file = readdir($handle) )){  
    if($file != '.' && $file != '..' && pathinfo("$folder/$file", PATHINFO_EXTENSION) == "jpg"){
        $file2 = str_replace("_mp4.jpg","",$file);
        $file3 = substr($file,0);
        //$filenames = directory(".","jpg");
        //foreach ($filenames as $value)
        //{
        echo '<li><a href="photobooth/photobooth/Michelle_Illona_Alexander/animated/'.$file2.'.mp4"><img src="photobooth/photobooth/Michelle_Illona_Alexander/animated/'.$file.'" width="300" title="" type="jpg"></a><br/></li>';  
        if(($i % 4) == 0){  
            echo '<br/><br/><br/><br/><br/><br/><br/><br/><br/>';
        }     
        $i++;
        if($i == 0){
            break;
        }
        //}
    }    
}  
?>
BlackNetworkBit
  • 768
  • 11
  • 21