首页 > 解决方案 > PHP exec() 运行代码但不能完全工作

问题描述

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

exec我使用的功能中comm -13 <(sort file_a) <(sort file_b) > output

当我运行我的 php 代码时,它会创建输出文件,但文件本身是空的。当我直接将命令复制并粘贴到终端时,它还会填充具有差异的文件,但不会在 php.ini 上填充我的输出文件。

部分代码;

exec('bash -c \'comm -13 <(sort "' . $path_d_raw.$least_recent_raw_file . '") <(sort "' . $path_d_raw.$most_recent_raw_file . '") > test.txt 2>&1\'', $output, $return);

$path_d_raw.$least_recent_raw_file and $path_d_raw.$most_recent_raw_file路径正确,/file 对其进行了一百次测试。

我也尝试过,shell_exec但无法以任何方式完成。

标签: phpbashexec

解决方案


您可以使用 filesize() 函数直接比较文件大小,也可以回显该值。

    <?php

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

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

?>

https://www.php.net/manual/ro/function.filesize.php


推荐阅读