2 ms·
I once made a somewhat similar error. Many years ago, when I was just learning PHP, I attempted to write a function to delete all the contents of a directory, i
by MattBearman 13y ago
I once made a somewhat similar error. Many years ago, when I was just learning PHP, I attempted to write a function to delete all the contents of a directory, including subdirectories.
I don't remember the exact code, but it was something like:
function clear_dir($dir) {
$contents = scandir($dir); // returns array of directory contents
foreach($dir => $file) {
if(is_dir($file)) {
clear_dir($file);
}
else {
unlink($file);
}
}
}
The function names may not all be correct, been a while since I PHP'd
Anyway, so the code looks like it should work. The problem is the scandir function result includes . and ..
So my little function would see .., recognise it as a directory, and recursively call itself on the parent of the directory I wanted to empty. I lost a fair few files before I realised what was happening. Luckily I had back ups, as this was before I started using SCMs.