Page 1 of 1

Working with files and directories

Posted: Wed Jun 03, 2009 5:24 am
by growlboy
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 :

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.';
}
?>
 
... 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.

Re: Working with files and directories

Posted: Wed Jun 03, 2009 6:26 am
by som3on3
Is directory chmod to 777 ?
you can also try

Code: Select all

 
exec('rm -rf ' . $ path_to_dir);
 

Re: Working with files and directories

Posted: Wed Jun 03, 2009 10:17 am
by mikemike
Ensure the owner is the same (including group ownership). For example, a Linux user 'mike' cannot delete directories created by Linux user 'andy', unless of course Mike has superuser permissions.

Re: Working with files and directories

Posted: Wed Jun 03, 2009 11:43 am
by growlboy
Thank you for your replies.

I know rm -rf commands , but I don't need 'em , I am learning PHP, so it's problem with PHP.


I am the owner of the scripts I use , example : http://127.0.0.1/~david/week.php . I am David ;)

Re: Working with files and directories

Posted: Wed Jun 03, 2009 4:48 pm
by growlboy
Somebody?

Re: Working with files and directories

Posted: Thu Jun 04, 2009 6:14 am
by growlboy
I'm repeating myself. Somebody? This is urgent!

Re: Working with files and directories

Posted: Thu Jun 04, 2009 6:25 am
by onion2k
growlboy wrote:This is urgent!
People here volunteer their time and knowledge. They are not compelled to help. Please don't try to hurry people when they're doing you a favour.

If it really is important that you have a level of service faster than people will provide for free the solution is simple - pay. We have a "Job Hunt" folder here you can post in if you'd like to advertise for someone to come in an fix this fast. Remember to include plenty of information about the job.

Re: Working with files and directories

Posted: Thu Jun 04, 2009 6:42 am
by growlboy
ok

Re: Working with files and directories

Posted: Thu Jun 04, 2009 4:06 pm
by Darhazer
Ok, perform a ls -la command in that dir and give us the output.
And before that, run this PHP code

Code: Select all

file_put_contents('testme.txt', 'test');
in the folder, so we can see the user which PHP uses to create the file
Make sure the folder itself have 777 permission