Skip to content
Advertisement

PHP exec command to write to server

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');
?>

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement