Search code examples
phparraysstringfilenamesglob

How to use PHP string endsWith to skip files from read?


I want to simple skip files with filename that ends with "@2x" and implement in this code:

$fullres = glob("gallery/*.*");
        for ($i=0; $i<count($fullres); $i++)

            {                           
                $num = $fullres[$i];                        
                echo '<a href="'.$num.'" ><img src="/slir/?w=60&amp;h=80&amp;c=3x4&amp;q=85&amp;i=/'.$num.'" alt=""  /></a>';
            }

Is it actually possible?


Solution

  • You shouldn't use substr() cuz than you are assuming that file name won't have any periods, instead use pathinfo

    <?php
    
    $fullres = glob("gallery/*.*");
    foreach($fullres as $num) {
    $fetch_file_name = pathinfo($num); //Fetch the file name with extension
    $match_str = substr($fetch_file_name['filename'], -3); //Crop the file name
    
       if($match_str != '@2x') {
          echo '<a href="'.$num.'" ><img src="/slir/?w=60&amp;h=80&amp;c=3x4&amp;q=85&amp;i=/'.$num.'" alt=""  /></a>';
       }
    }
    ?>