I am working third party integration for which new files generating automatically when my code is running.

But it required permission after creating the file.

I want to give the permission as a root user?

I have tried like this shell_exec("chmod -R 777 /run/folderName/filename");

But this is not working

  • From where you want to execute command. Controller file or root file? – Rohan Hapani Feb 19 at 13:11
  • from root file. – Vinod Kumar Feb 19 at 13:26

Try to use this below code from root file :


ini_set('max_execution_time', 0);
use \Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('\Magento\Framework\App\State');
$drive = $objectManager->get('\Magento\Framework\Shell\Driver');
$collections = $drive->execute('chmod -R 777 /run/folderName/filename',[]);

Hope, It will helpful for you.

| improve this answer | |
  • I am asking for sudo access via php script – Vinod Kumar Feb 19 at 13:32

Disclaimer: Try to avoid setting 777 on files and run functions as root in php due to security concerns.

But if you really want to use shell commands as root from PHP can you can do so like below. I also recommend using Rohans implementation instead of using native shell_exec to decouple your logic from the implementation.


shell_exec("echo mysudopassword | /usr/bin/sudo -S chown -R 655 /some/path/to/things");

As a side note you PHP has its over permission management functions, although not useful in your case since they will run as the PHP user. https://www.php.net/manual/en/function.chmod.php https://www.php.net/manual/en/function.chown.php

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.