2011-12-02 45 views
0

我試圖執行與shell_exec()從PHP shell命令:PHP了shell_exec不接受「>」參數

$output = shell_exec('python test.py > test.txt'); 

當我在命令行中運行python test.py > test.txt它的工作原理。當我試圖通過PHP做同樣的,沒有創建test.txt文件和命令似乎沒有運行。

shell_exec()當我不使用>,例如工作正常

$output = shell_exec('python test.py'); 
echo $output; 

我正在使用PHP 5.3.6(不在安全模式下)。任何幫助讚賞。

+1

http://php.net/manual/en/function.escapeshellcmd.php – jValdron

+1

請問你的Apache/PHP的用戶權限寫入到當前目錄和/或'test.txt'?看看你的'error.log'。 – mario

+0

你有適當的權限/ SELinux值在目錄和文件? – vdbuilder

回答

1

你有沒有做過的合法性檢查?像這樣的東西

<?php 
shell_exec('echo blah > /tmp/blah.txt;'); 
if(trim(file_get_contents('/tmp/blah.txt')) == 'blah') 
    echo 'It works' . PHP_EOL; 
else 
    echo 'Something is wrong' . PHP_EOL; 

此外,最糟糕的情況下,一個簡單的解決方法應該做的伎倆。

<?php 
file_put_contents('test.txt', exec('python test.py')); 

如果你需要較大的文件內容緩衝你可以得到W/fwrite et al.

誰知道,你可能不會給你寫信打算,儘量投入的絕對路徑測試目錄.txt