如何通过PHP在网页上执行sh文件


How to execute sh file by PHP from web page

/root/script/test.sh

echo 'test';

/var/www/html/sh.php

<?php
echo 'start'.PHP_EOL;
echo shell_exec('/bin/sh /root/script/test.sh');
echo 'end'.PHP_EOL

如果我在本地php sh.php上运行PHP,我得到了start test end,但如果我从url http://example.com/sh.php运行PHP,我只得到start end,脚本不会执行,但如果执行echo shell_exec('ls -al');,它会在网页上显示列表。

使用file_get_contents:尝试此操作

$path = file_get_contents('/root/script/test.sh');
echo shell_exec($path);