【问题标题】:shell command with options from php带有 php 选项的 shell 命令
【发布时间】:2017-07-20 09:34:24
【问题描述】:

我有这个代码:

$codif ="file --mime-encoding -b inputfile.txt";
$codif = shell_exec($codif);
$encode = "iconv --from-code=$codif --to-code=UTF-8 --output=tempfile.txt inputfile.txt";

我试过了

shell_exec($encode); //1
exec($encode); //2
system($encode); //3

我有这个只是为了看看生成的命令是什么:

echo $encode;

哪个输出这个:

iconv --from-code=iso-8859-1 --to-code=UTF-8 --output=tempfile.txt inputfile.txt

问题是执行命令的三种形式中的任何一种都会出现下一个错误:

sh: 2: --to-code=UTF-8: not found

在 shell 中执行输出命令时效果很好。 我还尝试将--to-code=UTF-8 更改为-t UTF-8,结果相同。 所以问题是,我做错了什么以及如何解决它?谢谢!

【问题讨论】:

    标签: php shell command iconv


    【解决方案1】:

    $codif 变量的末尾有一个新行,这把事情搞砸了。 这需要以某种方式剥离。 你可以试试这个,例如:

    $codif ="file --mime-encoding -b inputfile.txt|tr -d '\n'";
    $codif = shell_exec($codif);
    

    file 命令的管道输出到tr -d "\n" 将删除新行。您当然也可以在 php 代码中删除它。

    【讨论】:

    • 是的,你成功了!非常感谢!
    【解决方案2】:

    尝试将您的 $encode var 更改为:

    iconv -f $codif -t UTF-8 --output=tempfile.txt inputfile.txt

    输出类似:

    iconv -f iso-8859-1 -t UTF-8 --output=tempfile.txt inputfile.txt

    【讨论】:

    • 对不起,我之前没让你知道,是的,更改为固定值可以让它工作,但我需要编码检测部分,所以这就是失败的原因。
    • 无论如何,我也尝试将--from-code= 更改为-f,但这不起作用,但将$codif 变量更改为固定值iso-8859-1 使其工作。所以问题似乎是按时获取变量值以执行 de commad。
    猜你喜欢
    • 2019-10-11
    • 1970-01-01
    • 2014-05-28
    • 2015-10-02
    • 1970-01-01
    • 2016-06-23
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多