0

In my program I need to copy a file to a new file. I'm using the following php code to change the folder permission and copy existing file test.php to a new file (not already created) test_1.php.

exec('chmod -R 0777 /folder1/');
exec('sudo cp /folder1/test.php /folder/test_1.php');

But this is not creating and copying the test_1.php file.

Can anyone help me to fix this? Thanks in advance.

Jenz
  • 8,280
  • 7
  • 44
  • 77

2 Answers2

1

When you are doing:

exec('sudo cp /folder1/test.php /folder/test_1.php');

Does your php code has 'sudo' permissions to move files ? You need to set permissions accordingly.

Please refer this: Sudo in php exec

Community
  • 1
  • 1
informer
  • 821
  • 6
  • 18
-1

your problem is in the first command,

PHP does not have the permission to perform this :

exec('chmod -R 0777 /folder1/');

as long as PHP http server api is running -usually and unless you have not changed this- under www-data user which is has no permissions to perform tasks like this.

you need as an sysadmin to give that folder folder1 the 0777 permission first, then you may easily perform commands on it using php

another approach by giving www-data user the ownership of the folder,

but you will still have to perform this command as a super user [not recommended]

$ chown www-data folder1/
hassan
  • 7,812
  • 2
  • 25
  • 36