1

I am developing an application using Symfony2. The problem I have is that when creating a folder throe a function in a entity the folder created has no sudo privileges, I would like to know how would it be possible to create the folder directly with sudo permissions. this is my code:

 protected function getUploadDirMark()
    {
        // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
        return 'uploads/documents/'.$this->getIzenburua();
    }  

The 'uploads/documents/'.$this->getIzenburua(); folder has no sudo permissions, how can I create it with sudo privileges. Thanks.

Haritz
  • 1,702
  • 7
  • 31
  • 50

3 Answers3

1

If the regular chmod PHP function is not working, you can execute the chmod shell command with sudo permissions.

This is what it would look like in your Command class:

$uploadDirmark = $this->getUploadDirMark();
$dialog = $this->getHelperSet()->get('dialog');
$passwd = $dialog->ask($output, 'Please enter sudo password');
exec("echo $passwd | sudo -S chmod -R a+w $uploadDirmark");

This would make the directory and all its contents writable (and deletable) to all users. Modify the command as you see fit (see man chmod on the command line for more info). Opening it up like this is potentially dangerous, try putting the web server in the same group as the user that creates the files and use "g+w" instead of "a+w".

chiborg
  • 26,978
  • 14
  • 97
  • 115
  • `exec("echo $passwd |` is extremely unsafe; every user on the system can now read your password with `ps` while the command is running. If you absolutely have to pass a password to sudo, pipe it into `sudo`'s STDIN directly from PHP. – Martin von Wittich Aug 24 '18 at 12:04
0

doen't this work ? chmod()

Nikita Gopkalo
  • 559
  • 1
  • 6
  • 13
  • How could I write this? by putting return chmod('uploads/documents/'.$this->getIzenburua(), 777); It cant be done. It prompts a error. Any idea? – Haritz May 07 '12 at 15:33
0

Why would you need sudo privileges for that? Since your application is run by a server under a particular user, it will have no problems serving files or folders it created.

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
  • After creating the directory uploads/documents/'.$this->getIzenburua() I save some xml files in there and afterwards I load them using PHP DOM, the problem is this xml can not be loaded cause the privileges in the directory wont let. Any idea @elnur? – Haritz May 07 '12 at 16:16
  • using a PHP DOM script located in the entity repository. Any idea? – Haritz May 07 '12 at 16:37
  • I'm not getting it. Is your script run by the same PHP executable as the server itself? I mean, is it run by the webserver after saving the files or do you run the script separately from command line? – Elnur Abdurrakhimov May 07 '12 at 16:52