Gitignore Files disappearing from CodeIgniter Captcha Directory
I am using Git to manage the development and deployment of a website built using CodeIgniter and Tank Auth. In order to prevent git status from flagging captcha image files created during testing, I have placed a .gitignore file into the captcha directory. This file contains the single line:
*.jpg
However, the captcha plugin supplied with CodeIgniter is a bit overzealous when it comes to cleaning up expired captcha image files: it also deletes the .gitignore file. This happens even when the web-server does not have write access to the .gitignore file. On Linux (I don't know about Windows) to delete a file from a directory you only need write access to the directory, not to the file itself.
[See the update at the foot of this post for a much simpler fix than this one.]
My fix for this was to change the Codeigniter captcha plugin so it only deletes *.jpg files. In file system/captcha_pi.php I changed:
while($filename = @readdir($current_dir))
{
if ($filename != "." and $filename != ".." and $filename != "index.html")
{
$name = str_replace(".jpg", "", $filename);
if (($name + $expiration) < $now)
{
@unlink($img_path.$filename);
}
}
}
to:
while($filename = @readdir($current_dir))
{
$name = str_replace(".jpg", "", $filename, $count);
if ($count and ($name + $expiration) < $now)
{
@unlink($img_path.$filename);
}
}
I do not currently have the CodeIgniter cache or logging functions enabled, so I don't know if .gitignore files in the system/cache and system/logs directories will also suffer such deletions.
I've just realized that there is a much simpler solution to this problem: move the .gitignore file up to to the CodeIgniter root directory. I had somehow got the mistaken idea that the .gitignore file must be in the directory alongside the files that it tells Git to ignore. However, while watching this video by Scott Chacon, I noticed he had paths like log/*.log in his .gitignore file. So I reverted my change to the CodeIgniter captcha plugin, moved the .gitignore up one level, and changed it to contain:
captcha/*.jpg
Much better!
Reader Comments