Where you have:
while ($file = $mydir->read()) !== false)
while (false !== ($file = readdir($mydir)))
That's what made it work for me. Please note that that I used:
© 2014, O’Reilly Media, Inc.
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.