-1

How to create a directory with php and chmod it to 0777? I have searched other stack articles related to chmod via PHP, all are file related, not directory...

public function store(Document $document) {

    if (!isset($document->id)) { $document->id = $this->generateId(); }

        //This is my guess how to do it..
        $this->path      = $this->path . DIRECTORY_SEPARATOR
        if (!file_exists($this->path)) {
            mkdir($this->path);
            chmod($this->path, 0777);
        }
    //My guess does not work

    $path    = $this->getPathForDocument($document->id);
    $data    = $this->formatter->encode((array) $document);

    return file_put_contents($path, $data);
}
Federkun
  • 36,084
  • 8
  • 78
  • 90
WerewolF
  • 39
  • 7
  • 1
    `mkdir($this->path, 0777);`, which is the default. What's your real problem? What's not working? – Federkun Aug 04 '18 at 14:13
  • I don't think you need the `chmod($this->path, 0777);` because mkdir has default 2nd parameter with `0777` i.e The mode is 0777 by default, which means the widest possible access. We can change it. But in your case no need to do that. – A l w a y s S u n n y Aug 04 '18 at 14:15

2 Answers2

1

You have to ensure that your current web server user has write permissions on that folder where you want to run the mkdir command. If not, you can’t use PHP‘s mkdir method. If you can’t create a folder you can also not change it’s ownership.

Therefore, first make sure that your Webserver user has appropriate permissions. Then run your code above.

Doğan Uçar
  • 166
  • 3
  • 15
  • 1
    It would be helpful for visitors if you also elaborate on how to find out web server user and directory permissions. – Ahmed Numaan Aug 04 '18 at 16:12
  • "What's not working?" If there is no folder in the directory it does not create one. If there is no folder it cannot write the file. ..elaborate on how to find out web server user and directory permissions." Agreed. When i chmod the directory with WinSCP it says on new form .json file creation that the owner is www-data which is nothing that is part of any file in the system i can see. Everything else is marked as owner root. – WerewolF Aug 04 '18 at 21:10
  • @dogano how do I "ensure that your current web server user has write permissions on that folder"? – WerewolF Aug 04 '18 at 21:19
  • @Federkun "What's not working?" If there is no folder in the directory it does not create one. If there is no folder it cannot write the file – WerewolF Aug 04 '18 at 21:20
  • @AhmedNumaan I was thinking this exactly... how to find out web server user and directory permissions? – WerewolF Aug 04 '18 at 21:21
  • @WerewolF There is this tutorial which will help you understand file and directory permissions: http://catcode.com/teachmod/index.html – Ahmed Numaan Aug 05 '18 at 05:53
  • @WerewolF have a look here: http://php.net/manual/de/function.is-writable.php – Doğan Uçar Aug 05 '18 at 20:00
0

Stumbled on this: PHP mkdir: Permission denied problem

They suggested this: chown -R www-data:www-data /var/www/example.com/public_html/ chmod -R g+rw /var/www/example.com/public_html/

It actually worked.

Thanks :)

WerewolF
  • 39
  • 7