Working with files and directories
Posted: Wed Jun 03, 2009 5:24 am
Hi everybody!
I'm learning PHP. In my learning I came to chapter "Working with files and directories" . So here's the problem. I use openSUSE 11.0 (Linux) and in some way it seems like I don't have permission to access files . Here's the script, check it out :
... this is the example from the book. It is used to delete directory and all files in it. I get a message 'ERROR: Directory could not be removed.' ... and every time I try to edit any outer file with any script, with every try of editing I get an error message. So I guess it has something to do with permissions , or not? Files that I try to edit aren't locked and are easily edited in any other way . Please help.
I'm learning PHP. In my learning I came to chapter "Working with files and directories" . So here's the problem. I use openSUSE 11.0 (Linux) and in some way it seems like I don't have permission to access files . Here's the script, check it out :
Code: Select all
<?php
// function definition
// remove all files in a directory
function removeDir($dir) {
if (file_exists($dir)) {
// create directory pointer
$dp = opendir($dir) or die ('ERROR: Cannot open directory');
// read directory contents
// delete files found
// call itself recursively if directories found
while ($file = readdir($dp)) {
if ($file != '.' && $file != '..') {
if (is_file("$dir/$file")) {
unlink("$dir/$file");
} else if (is_dir("$dir/$file")) {
removeDir("$dir/$file");
}
}
}
// close directory pointer
// remove now-empty directory
closedir($dp);
if (rmdir($dir)) {
return true;
} else {
return false;
}
}
}
// delete directory and all children
if (file_exists('psyco-1.6')) {
if (removeDir('psyco-1.6')) {
echo 'Directory successfully removed.';
} else {
echo 'ERROR: Directory could not be removed.';
}
} else {
echo 'ERROR: Directory does not exist.';
}
?>