I have this sort function which scans a directory and lists all jpg
files, how could I make it to sort only jpg
files whose file-name match a specified keyword, for example to find and sort all jpg
files whose name includes the keyword "toys"
.
$a_img[] = array(); // return values
$keyword = "toys"; // your keyword
$allowed_types = array('jpg'); // list of filetypes you want to show
$dimg = opendir($imgdir);
while($imgfile = readdir($dimg)) {
// check to see if filename contains keyword
if(false!==strpos($keyword, $imgfile)){
//check file extension
$extension = strtolower(substr($imgfile, strrpos($imgfile, ".")+1));
if (in_array($extension, $allowed_types)) {
// add file to your array
$a_img[] = $imgfile;
}
}
}
// sort alphabetically by filename
sort($a_img);
$totimg = count($a_img); // total image number
for($x=0; $x < $totimg; $x++)
{
$size = getimagesize($imgdir.'/'.$a_img[$x]);
// do whatever
echo $a_img[$x];
}