0

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

2
  • From where you want to execute command. Controller file or root file? Commented Feb 19, 2020 at 13:11
  • from root file. Commented Feb 19, 2020 at 13:26

2 Answers 2

0

Try to use this below code from root file :

<?php

error_reporting(1);
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');
$state->setAreaCode('frontend');
$drive = $objectManager->get('\Magento\Framework\Shell\Driver');
$collections = $drive->execute('chmod -R 777 /run/folderName/filename',[]);

Hope, It will helpful for you.

1
  • I am asking for sudo access via php script Commented Feb 19, 2020 at 13:32
0

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.

<?php

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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