Is there are function to determine whether PHP is run as FTP user or Apache?

Discussion in 'PHP' started by norfstar, Aug 14, 2009.

  1. #1
    On some hosts I use, when PHP creates a file, it creates it as owned by the FTP user (which is how I like it), but on other hosts it is created as being owned by Apache or nobody or some other user. Is there are function to work out what the setting is? So I could do something like:

    if (is_ftp_user()){
    // Create some files, directories etc.
    } else {
    // Prompt to upload files, directories by FTP
    }
    Code (markup):

     
    norfstar, Aug 14, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    Usually, if php is run via CGI it will run as the user. If apache, it will run under nobody or apache.
     
    jestep, Aug 14, 2009 IP
  3. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #3
    Running PHP as user also often done with suPHP.

    You can try to get current user under *nix using whoami command via backticks or shell_exec function (could be disabled in safe mode etc.):

    <?
    echo 'whoami: '.`whoami`;
    ?>
    PHP:
     
    wmtips, Aug 14, 2009 IP
    norfstar likes this.
  4. norfstar

    norfstar Peon

    Messages:
    1,154
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks, I ended up using code like the following:

    	$phpRunsAs = trim(`whoami`);
    $thisFileOwner = posix_getpwuid(fileowner('/path/to/thisfile.php'));
    if ($phpRunsAs != $thisFileOwner['name']){
    	// Make directories etc.
    } else {
    	// Don't do the stuff, but echo a notice to the user to do it in FTP
    }
    PHP:
     
    norfstar, Aug 14, 2009 IP
  5. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #5
    Yeah. To avoid hardcoding you could also replace '/path/to/thisfile.php' with __FILE__ constant.
     
    wmtips, Aug 14, 2009 IP