0

So I have a bash script I want to run in php that has arguments but at the moment I cant even get PHP to write to the machine. As a basic test I tried the touch command to no success. I'm new to PHP so any help would be great. I don't understand whats wrong here. I've tried:

<?php 
shell_exec('touch /var/www/html/test.txt');
?>

<?php 
exec('touch /var/www/html/test.txt');
?>

<?php 
system('touch /var/www/html/test.txt');
?>

<?php 
passthru('touch /var/www/html/test.txt');
?>
8
  • What errors are you getting? Commented Jun 4, 2017 at 5:27
  • None, just not generating the file. Commented Jun 4, 2017 at 5:28
  • Make sure you are displaying all errors. Put this after your opening PHP tag: error_reporting(E_ALL); ini_set('display_errors', 1); Commented Jun 4, 2017 at 5:29
  • do you have the rights to write in that path? Commented Jun 4, 2017 at 5:31
  • Sorry but i don't know where or how to look for the errors. And I do have the permission to the path. Commented Jun 4, 2017 at 5:34

1 Answer 1

1

Difficult to answer this without more information. But, normally touch should create a new file. The most common reason is lack of permissions.

Normally, webserver runs with user www-data. Check and see if permissions are not set for that user.

You can simply run the command

addgroup www-data

That will error out if www-data has right permissions. Otherwise, things might just start working. Also restart apache after this for safety purposes.

/etc/init.d/apache2 restart

Alternatively, in the system command add a 2nd variable like this.

system('touch /var/www/html/test.txt', $retval);

$retval will contain the status of the error. Will help you debug.

You can also run a tail on the apache error log like this.

tail -f /var/log/apache2/error.log 

It should throw errors , if there is a problem.

Sign up to request clarification or add additional context in comments.

2 Comments

Cheers mate, didn't realise that it has its own group.
Pleasure Man. Came to know this recently.

Your Answer

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