How to generate a .htaccess and .htpasswd with PHP

Ever had a moment where you needed to secure a directory but didn’t have the htpasswd tool available?

The following script simply generates a .htaccess and .htpasswd file to secure a directory.