Shell commands through PHP - file permissions?

Discussion in 'PHP' started by sirgogo, Jul 22, 2011.

  1. #1
    Hi all,

    So I'm trying to delete files on my server via a PHP script/html forum. This is the script I got right now:
    <?php
    //$databaseusername = $_POST['user'];
    //$databasepassword = $_POST['password'];
    $count = $_POST['count'];
    echo "the count is: $count <br />";
    
    for ($i=1; $i<100; $i++)
    {
    	$deletion = $_POST["fax-$i-delete"];
    	if ($deletion != NULL)
    	{
    		$filename = $deletion;
    		$filename = substr($filename, 0 , strlen($filename) - 4 );
    		echo ''.nl2br(`rm -rf ../faxes/recvq/pdf/$filename.pdf`);
    		echo ''.nl2br(`rm -rf ../faxes/recvq/$filename.tif`);
    		echo "Deleted file $filename.pdf and $filename.tif <br />";
    	}
    }
    
    ?>
    Code (markup):
    The pdf deletes wonderfully, but the tif doesn't. I'm thinking this has to do with file permissions, so here's a "ls -l" on the relevant directories.

    
    [abhe@catbert pdf]$ ls -l
    total 44
    -rw-r--r-- 1 apache apache 44573 Jul 22 11:07 fax000002090.pdf
    [abhe@catbert pdf]$ cd ..
    [abhe@catbert recvq]$ ls -l
    total 188
    -rwxrwxrwx 1 uucp uucp 43782 Jul 21 09:56 fax000002090.tif
    -rwxrwxrwx 1 uucp uucp 57528 Jul 21 11:06 fax000002091.tif
    drwxrwxrwx 2 root root  4096 Jul 22 11:07 pdf
    drwxrwxrwx 2 root root  4096 Jul 22 11:02 publicpdf
    -rwxr-xr-x 1 uucp uucp     4 Jul 22 08:17 seqf
    -rwxr-xr-x 1 root root     4 Jul 20 11:31 seqf-backup
    -rwxr-xr-x 1 root root 54784 Oct 31  2010 Thumbs.db
    drwxr-xr-x 2 root root  4096 Oct 31  2010 viewed
    [abhe@catbert recvq]$ 
    
    Code (markup):
    Running commands via PHP authenticates as the apache user. I made everything in this recvq folder 0777 permissions using chmod. Not sure what the problem is :/.

    Any ideas?
     
    sirgogo, Jul 22, 2011 IP
  2. seni2com

    seni2com Greenhorn

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    You want to change the permissions of this files ?
     
    seni2com, Jul 22, 2011 IP
  3. suryawl

    suryawl Peon

    Messages:
    54
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #3
    if you just want to delete the file, you can use unlink(path_to_file . file_name);
    i don't see why you need to do that with shell command
     
    suryawl, Jul 22, 2011 IP
  4. organicCyborg

    organicCyborg Peon

    Messages:
    330
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The reason your PDF deletes ok is because it's owned by the apache user and group--which is what PHP executes commands like this under.

    
    chown apache:apache *.tif 
    
    Code (markup):
    In the directory with the tifs should do the trick. Also, don't forget to escape that $filename input, since you are executing commands on your sister with it.
     
    organicCyborg, Jul 23, 2011 IP
  5. sirgogo

    sirgogo Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for the replies.

    I can do it via ssh terminal, but the purpose of the script is to have a web interface or managing faxes. The fax program hylafax writes the incoming faxes to .tif files by default. Tif's convert to pdf's using another script. So I'm trying to be able to delete pdf's and tif's via web.

    Sweet, didn't know 'bout that. I'll give that a shot, seems safer too. Can I do that even for files owned by root (see below)?


    So, I can't delete even if files have universal rwx permissions (0777)? Dang. But yeah, can't use a chown, because the idea is to have a web interface and not manage through server-side scripts. Tif's are generated on the fly (with each incoming fax) so I would have to mess with the permissions of hylafax (I thought I could fix by assigning chmod -R 0777 to all the files in the recvq (main) directory.
     
    sirgogo, Jul 23, 2011 IP
  6. sirgogo

    sirgogo Peon

    Messages:
    19
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Can I bump10char?

    Also, unlink doesn't work suryawl, since apache must own the directory in order to do that. My directories are owned by uucp or root, with rwx permissions for most things.
     
    sirgogo, Jul 25, 2011 IP