![](/img/trans.png)
[英]php sudo shell_exec command runs from command line, but not from browser
[英]PHP. Different encoding when calling shell_exec from command line and browser
我有這個php腳本:
<?php
$cmd_desformat = "/usr/local/bin/process /tmp/input.txt > /tmp/output.txt";
shell_exec($cmd_desformat);
其中input.txt是UTF-8文件(用“ file -i”檢查),其中包含:
Buenos días
和/ usr / local / bin / process是來自第三方的二進制可執行文件,我已廣泛使用它並且從未遇到過此問題。
好吧,問題是當我從瀏覽器執行此操作時:
http://localhost/test.php
結果output.txt是包含以下內容的US-ASCII文件:
Buenos d?as [][
但是,當我從命令行執行此操作時:
php test.php
結果output.txt是具有預期值的UTF-8文件:
Buenos días [][
我試圖使用用戶www-data
從命令行執行命令,以查看是否可以復制瀏覽器行為,但是結果還是正確的。 我也嘗試使用exec
而不是shell_exec
但是結果是相同的。 還嘗試使用Firefox和Chrome。
從瀏覽器調用時,我需要它工作。 有任何想法嗎?
PHP CLI環境與shell_exec
環境不同。 如何設法使您的內容以正確的方式返回有兩種可能性。
最簡單的方法是通過在shell_exec
調用中調用env -i
來重置環境。
<?php
$cmd_desformat = "env -i /usr/local/bin/process /tmp/input.txt > /tmp/output.txt";
shell_exec($cmd_desformat);
如果未正確設置默認環境,則此方法可能無效。 如果是這種情況,則可能需要使用putenv()
顯式設置。
<?php
putenv('LANG=en_US.UTF-8'); // Set this to the language you need
$cmd_desformat = "/usr/local/bin/process /tmp/input.txt > /tmp/output.txt";
shell_exec($cmd_desformat);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.