5

I have a php that creates a file that needs to be executable (it's a batch file that needs to be run by the system). For some reason, even though the file is owned by apache and php is running as apache, and the file IS created, the script dies at the chmod line. What do I need to configure differently to allow the php to chmod the file it creates? Two lines above it happily creates a directory FOR this file which it chmods to 755 right as it creates it. Am I missing something obvious?

my chmod line looks like this:

    $uploadFilePath = "./path/to/file/";
    if(!is_dir($uploadFilePath)){
          mkdir($uploadFilePath, 0777 , true) or die("ERROR:can't create directory '$uploadFilePath'");
    }
        ...
    //write batch file
      ...
    chmod ($uploadFilePath . 'sftp.batch' ,0777 ) or die ("\ncan't chmod  " . $uploadFilePath . 'sftp.batch');
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • I don't know what that means... googling sticky bits... – Yevgeny Simkin Apr 18 '11 at 01:14
  • 1
    are you sure you are using the chmod right? it takes a octal rather than decimal value. – Uku Loskit Apr 18 '11 at 01:16
  • 1
    Why not just pass the filename to the interpreter? – Ignacio Vazquez-Abrams Apr 18 '11 at 01:16
  • editing question to include my chmod line... – Yevgeny Simkin Apr 18 '11 at 01:21
  • Yes, "/path/php /path/file.php" is the way to go - IMHO. – d-_-b Apr 18 '11 at 01:21
  • sorry, I don't mean to be dense, but I'm just not understanding what you guys are suggesting. I'm happy to read the docs on how this should be done, but I just don't see anything relevant in the PHP documentation. – Yevgeny Simkin Apr 18 '11 at 01:25
  • could you post the approriate ls -la outputs? – Uku Loskit Apr 18 '11 at 01:36
  • @Dr.Dredel, I really don't see anything wrong here!? Maybe it's something with wrong file owner? Have you tried to call [stat()](http://www.php.net/manual/en/function.stat.php) against `$uploadFilePath . 'sftp.batch'` and get information about file? Which OS you use? – Wh1T3h4Ck5 Apr 18 '11 at 01:38
  • this is actually happening both on my mac (running MAMP) and up on the server (not sure what flavor of linux is up there). I must be missing something really obvious, but I can't for the life of me see what it might be. When I manually execute these commands myself at the command line, they work fine. – Yevgeny Simkin Apr 18 '11 at 01:56
  • @Dr.Dredel: Could you try creating the directory manually (as user apache, nobody, or some such), then chmod the directory to set the sticky bit? I may be showing my age here, but that was how this was done back in the 1990s. – phooji Apr 18 '11 at 01:59
  • @phooji, I could try that, but ultimately it would be pointless since the process has to create the directories and then populate them with these files, but I guess it would be informative to know if that's what the issue is. All the directories appear to be chmoded correctly, though. – Yevgeny Simkin Apr 18 '11 at 02:01

1 Answers1

2

Most probably due to umask. Try setting it to 0 prior to the chmod.

mhitza
  • 5,709
  • 2
  • 29
  • 52
  • 0 made things very odd, but 0077, followed by a chmod to 0755 did the trick! However, the PHP still refuses to actually execute the file! GRRR – Yevgeny Simkin Apr 18 '11 at 02:30
  • I'm going to go ahead and give you the checkmark cause at least my chmoding problem is solved... I'll have to post the "why won't the php execute my batch file" problem in an alternate question :) – Yevgeny Simkin Apr 18 '11 at 02:31