/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);