PHP exec()运行代码,但不执行';我不能完全工作

PHP exec()运行代码,但不执行';我不能完全工作,php,bash,exec,Php,Bash,Exec,在代码的某些部分,我比较了两个文件,并使用exec()函数将差异输出到另一个文件 在exec函数中,我使用comm-13可以使用filesize()函数直接比较文件大小,也可以回显值 <?php // outputs e.g. somefile.txt: 1024 bytes $filename = 'somefile.txt'; echo $filename . ': ' . filesize($filename) . ' bytes'; ?> 我想这是个逃避

在代码的某些部分,我比较了两个文件,并使用
exec()
函数将差异输出到另一个文件


exec
函数中,我使用
comm-13可以使用filesize()函数直接比较文件大小,也可以回显值

    <?php

// outputs e.g.  somefile.txt: 1024 bytes

$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';

?>


我想这是个逃避问题。您应该使用转义参数和(在本例中)命令。试试这个:

<?php
$command = sprintf(
    "comm -13 <(sort %s) <(sort %s) > test.txt 2>&1",
    escapeshellarg($path_d_raw . $least_recent_raw_file),
    escapeshellarg($path_d_raw . $most_recent_raw_file)
);
$escaped_command = escapeshellarg($command);
exec("bash -c $escaped_command", $output, $return);

谢谢@miken32,代码仍然执行,没有任何问题,但它仍然不会将差异填充到新文件中。我检查了几次权限问题,但它只创建了
test.txt
文件,但没有填充它。这不需要做任何关于文件大小的事情:)它是关于比较两个文件并打印差异
<?php
$command = sprintf(
    "comm -13 <(sort %s) <(sort %s) > test.txt 2>&1",
    escapeshellarg($path_d_raw . $least_recent_raw_file),
    escapeshellarg($path_d_raw . $most_recent_raw_file)
);
$escaped_command = escapeshellarg($command);
exec("bash -c $escaped_command", $output, $return);