How to catch error during mass sort of files that constantly change

59 Views Asked by At
array_multisort(array_map('filemtime', ($files = glob("*.json"))), SORT_DESC, $files);

This is the most efficient method to sort 45k json files I have found so far. Works great, except in my case the files are constantly being changed by a game server so if a file gets deleted during this sort execution I get a error

String: filemtime(): stat failed for blah.json

Can I handle this with a try and catch exception? I am not having any luck searching for a solution.

1

There are 1 best solutions below

0
kmoser On

Suppress the error if you don't mind receiving false on failure for certain files:

array_multisort(array_map(function($file) { return @filemtime($file); }, ($files = glob("*.json"))), SORT_DESC, $files);