0

firstly I'd like to tell you that I know there are some answers already for this problem, but I don't know PHP, so I'd be really glad if you could help me.

So I have my website directory divided like that

  1. css
  2. images
    1. abandonedhotel
    2. katka
  3. js
  4. php

Well, what I'd like to know if there's possibility for .php file in "php" folter to access the "images" folder and subfolders and count the .jpg files there. Also if there could be some exclusion for "title.jpg" because I don't need that to count in. Also if is it possible to list those .jpg files (again without title.jgp) that'd be cool.

Thanks in advance to anybody who helps me.

Danny
  • 61
  • 6

1 Answers1

1

Could be something around the lines of:

<?php

function endsWith($haystack, $needle) {
    // search forward starting from end minus needle length characters
    return $needle === "" || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== FALSE);
}

function rsearch($folder, $pattern) {
    $dir = new RecursiveDirectoryIterator($folder);
    $ite = new RecursiveIteratorIterator($dir);
    $files = new RegexIterator($ite, $pattern, RegexIterator::GET_MATCH);
    $fileList = array();
    foreach($files as $file) {
        $fileList = array_merge($fileList, $file);
    }
    return $fileList;
}

$count = 0;
foreach(rsearch("../images/", "_.*jpg_iU") as $file) {
  if(!endsWith($file, "title.jpg")) {
    $count++;
    // output $file
  }
}

?>

-> php glob - scan in subfolders for a file

-> startsWith() and endsWith() functions in PHP

Community
  • 1
  • 1
Tobias
  • 9,170
  • 3
  • 24
  • 30