I need some help, how can I execute a shell command in a php file? on how to display the content of a file in a webpage.

I tested this script, it works

<?php$output = shell_exec('ls /var/www/vhosts/resame');echo "<pre>$output</pre>";?>

but this script, did not display anything.

<?php$output = shell_exec('cat /var/log/mail.log');echo "<pre>$output</pre>";?>
2

Best Answer


Run this command first (based on your apache user)

CHMOD 777 /var/log/mail.log

If apache user is nobodyCHOWN nobody /var/log/mail.log

If apache user is www-dataCHOWN www-data /var/log/mail.log

Then use the following to display data on the web page.

<?php$output = shell_exec('cat /var/log/mail.log 2>&1');echo "<pre>$output</pre>";?>

Good one @DJ, let me also add that permission 777 for /var/log/mail.log is not necessary since you are only reading the file content, permission 644 should be enough. I do not need to over-flog the security implication changing your public file permission to 777.