<html>
<head><title>Result</title></head>
<body>
<?php
$source=$_POST['source'];
echo $source;
shell_exec('echo $source > /home/slim/tmp/main.c');
?>
</body>
</html>
这段代码运行之后,怎么没有生成main.c文件?

解决方案 »

  1.   

    注意引号的用法。把代码改为:
    <html>
    <head><title>Result</title></head>
    <body>
        <?php
            $source=$_POST['source'];
            echo $source;
            shell_exec('echo '.$source.' > /home/slim/tmp/main.c');//或者直接将单引号改为双引号(双引号里的$是不解释的)
        ?>
    </body>
    </html>还有,我想提醒,echo这种用法,要很小心,因为如果$source里有“>>、<<、>、<”等重定向符号或者其它特殊符号,你这个生成main.C是不正确的,甚至生成失败。建议用file_put_contents,这样不用经过Linux的shell。