Working with files and directories

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
growlboy
Forum Newbie
Posts: 6
Joined: Wed Jun 03, 2009 4:47 am

Working with files and directories

Post 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.
som3on3
Forum Newbie
Posts: 7
Joined: Tue Jun 02, 2009 6:26 am

Re: Working with files and directories

Post by som3on3 »

Is directory chmod to 777 ?
you can also try

Code: Select all

 
exec('rm -rf ' . $ path_to_dir);
 
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Working with files and directories

Post 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.
User avatar
growlboy
Forum Newbie
Posts: 6
Joined: Wed Jun 03, 2009 4:47 am

Re: Working with files and directories

Post 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 ;)
User avatar
growlboy
Forum Newbie
Posts: 6
Joined: Wed Jun 03, 2009 4:47 am

Re: Working with files and directories

Post by growlboy »

Somebody?
User avatar
growlboy
Forum Newbie
Posts: 6
Joined: Wed Jun 03, 2009 4:47 am

Re: Working with files and directories

Post by growlboy »

I'm repeating myself. Somebody? This is urgent!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Working with files and directories

Post 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.
User avatar
growlboy
Forum Newbie
Posts: 6
Joined: Wed Jun 03, 2009 4:47 am

Re: Working with files and directories

Post by growlboy »

ok
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Working with files and directories

Post 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
Post Reply