Jump to content

opendir/readdir HELP!


willpower

Recommended Posts

Hi

 

I have some code which opens a directory and reads the contents.  In this case it performs some image manipulationof the jpgs it encounters.

 

BUT

 

The problem is that it does this based on the file structure which is by default, alphabetical . I need it to be numeric.

 

ie

 

DSC0001.jpg DSC0002.jpg etc

 

currently i get

 

DSC0001.jpg  DSC0011.jpg  DSC0002.jpg

 

etc etc

 

Any help?????? I am in a real pickle here.

Link to comment
https://forums.phpfreaks.com/topic/134429-opendirreaddir-help/
Share on other sites

<?php
  $files = array();
  $sort = array();
  foreach(scandir('images/') as $file){
    if(substr($file,-4) != '.jpg')
      continue;
    $files[] = $file;
    $sort[] = preg_replace('/[^\d]/','',$file);
  }
  array_multisort($sort,$files);
  print_r($files);
?>

Link to comment
https://forums.phpfreaks.com/topic/134429-opendirreaddir-help/#findComment-700012
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.