A simple .htaccess file to protect an entire directory might look something like this ...
AuthName "mydomain.com"
AuthType Basic
AuthUserFile /path/to/htpasswd/files/passwordfile
require valid-user
Then the password file '/path/to/htpasswd/files/passwordfile' contains a username and an MD5 hash of the password seperated with a colon. It looks something like this.
joe:Hal8GV3lHCkak
fred:SG8zpSQJfpkcc
Provided apache is coonfigured properly, you can create a script to write the htpasswd file and MD5 the password and protect your directory. It is not too secure to keep the password file in your web root though.
Also, don't forget to put an -la flag in your FTP client so you can see the .htaccess file when you load it.
Take a look at PHP's HTTP auth routine, it may work for you as well.
http://www.php.net/manual/en/features.http-auth.php