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');
?>
  • What errors are you getting? –  Jun 04 '17 at 05:27
  • None, just not generating the file. –  Jun 04 '17 at 05:28
  • Make sure you are displaying all errors. Put this after your opening PHP tag: `error_reporting(E_ALL); ini_set('display_errors', 1); ` –  Jun 04 '17 at 05:29
  • do you have the rights to write in that path? – Alan Torres Jun 04 '17 at 05:31
  • Sorry but i don't know where or how to look for the errors. And I do have the permission to the path. –  Jun 04 '17 at 05:34
  • @Zahkc Read this (https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) about how to get errors to display in your page. Basically just add the snippet, then run the script. For some reason you're not getting the expected result. This means you need to visualize any errors PHP is throwing, if the problem is with PHP, it will tell you. –  Jun 04 '17 at 05:39
  • Have you tried the command directly? Is it working? – Umashankar Das Jun 04 '17 at 05:45
  • I've added the lines to the php file but I don't know where the errors would show up? I'm running the server and testing it by loading the page. (I don't even know if that wrong). The command does work normal outside php. –  Jun 04 '17 at 05:51

1 Answers1

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.

Umashankar Das
  • 601
  • 4
  • 12