You're calling readdir()
only when it's an invalid resource, i.e. - when it failed to open the directory:
if (!is_dir($upgraded_folder_path) && !($handle = opendir($upgraded_folder_path)))
You can only read an existing directory and one that you can open. If you remove the !
from both of the conditions in that if
-statement, it should work fine. Albeit, you also need to make sure the directory exists and your user has permission to read from it.