specifying pathe for filesystem commands

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
justravis
Forum Commoner
Posts: 53
Joined: Mon Dec 16, 2002 3:18 am
Location: San Diego, CA
Contact:

specifying pathe for filesystem commands

Post by justravis »

Consider these paths:
~/www/admin/webmaster/file_cmds.php
~/www/content/

I'm trying to use rmdir() & exec(), in file_cmds.php to manipulate files & folders in the content dir. It's not working. I can only manipulate files in the same dir as the script.

How should I specify the path?

Failed attempts:
~/www/content/
/home/user/www/content/

Here is an example of code i'm using:

Code: Select all

<?php

    if(file_exists($dir))
    {
        #@ prevents from errors being outputted when rmdir() fails.
        $emptydir=@rmdir("$dir");

        if(!$emptydir)
        {
            exec("rm -r $dir");
        }
    }

?>
User avatar
Elmseeker
Forum Contributor
Posts: 132
Joined: Sun Dec 22, 2002 5:48 am
Location: Worcester, MA

Post by Elmseeker »

Don't use relative path's use full path's to your files, try changing:

~/www/admin/webmaster/file_cmds.php
~/www/content/

to:

/full/path/to/www/admin/webmaster/file_cmds.php
/full/path/to/www/content/
User avatar
Johnm
Forum Contributor
Posts: 344
Joined: Mon May 13, 2002 12:05 pm
Location: Michigan, USA
Contact:

Post by Johnm »

Also, ensure that the webserver has the proper permissions to do that too.


John M
justravis
Forum Commoner
Posts: 53
Joined: Mon Dec 16, 2002 3:18 am
Location: San Diego, CA
Contact:

Post by justravis »

If the webserver was not configured properly, would the scripts even manipulate files and folders in it's own dir?
justravis
Forum Commoner
Posts: 53
Joined: Mon Dec 16, 2002 3:18 am
Location: San Diego, CA
Contact:

problem lies in functions returning fals

Post by justravis »

Isolated problem...functions execute, but the functions still return false. Any ideas why?

I have error messages that print out for these scenarios:

Code: Select all

<?php
$mkdirsuccess=dircreate($pardir, $newdir);
$deldirsuccess= dirdel($dir);

if(!$mkdirsuccess)
{
     echo error;
}
if(!$deldirsuccess)
{
     echo error;
}
?>
Here are my functions:

Code: Select all

#Creates directories if they do not exist & changes thier permissions if they do.
function dircreate($pardir, $newdir)
{
        $ogmask=umask(0);

        if(!file_exists("$pardir/$newdir"))
        {
                $mkdirsuccess=mkdir("$pardir/$newdir", 0777);

                return $mkdirsuccess;
        }
        else
        {
                $chgpermssuccess=chmod("$pardir/$newdir", 0777);

                $chgpermssuccess;
        }

        $newmask=umask($ogmask);
}

#Deletes directories empty or not.
#If directory does not exist, it still returns true.
function dirdel($dir)
{
        if(file_exists($dir))
        {
                #@ prevents from errors being outputted when rmdir() fails.
                $emptydir=@rmdir($dir);

                if(!$emptydir)
                {
                        $execsuccess=exec("rm -rf $dir");

                        return $execsuccess;
                }
                else
                {
                        return $emptydir;
                }
        }
        else
        {
                $alreadydel++;

                return $alreadydel;
        }
}
justravis
Forum Commoner
Posts: 53
Joined: Mon Dec 16, 2002 3:18 am
Location: San Diego, CA
Contact:

PROBLEM SOLVED

Post by justravis »

Yesterday I isolated the problem even further to the chmod portion of the direcreate() function. If the folder already exists, the function is supposed to ensure the folder has the correct permissions.

I spent eternity trying to prove my theory that chmod() returns false if the folder is already the permissions you are trying to set. WHY ELSE WOULD THE CHMOD PORTION RETURN FALSE???

Well I found the answer to that question when I finally decided to refer back to the dircreate() function:

Code: Select all

<?php
else 
        { 
                $chgpermssuccess=chmod("$pardir/$newdir", 0777); 

                $chgpermssuccess; 
        } 
?>
"$chgpermssuccess;" SHOULD BE "return $chgpermssuccess;"

HMMM>>>MAYBE THAT IT IS WHY IT WAS RETURNING FALSE

DUHHH
justravis
Forum Commoner
Posts: 53
Joined: Mon Dec 16, 2002 3:18 am
Location: San Diego, CA
Contact:

PROBLEM SOLVED

Post by justravis »

Yesterday I isolated the problem even further to the chmod portion of the direcreate() function. If the folder already exists, the function is supposed to ensure the folder has the correct permissions.

I spent eternity trying to prove my theory that chmod() returns false if the folder is already the permissions you are trying to set. WHY ELSE WOULD THE CHMOD PORTION RETURN FALSE???

Well I found the answer to that question when I finally decided to refer back to the dircreate() function:

Code: Select all

<?php
else 
        { 
                $chgpermssuccess=chmod("$pardir/$newdir", 0777); 

                $chgpermssuccess; 
        } 
?>
"$chgpermssuccess;" SHOULD BE "return $chgpermssuccess;"

HMMM>>>MAYBE THAT IT IS WHY IT WAS RETURNING FALSE

DUHHH

Anyway, I thought I would be courteous enough to post the solution found. Even if it was just an oversight on my part. You never know if someone may refer to this for an answer to a similar problem.
Post Reply