2014-09-20 79 views
0

我試圖用shell_execexec而不是proc_open來編譯一個Sass(.scss)文件的內容。不要問我爲什麼不通過文件本身或使用proc_open,這裏的目標是通過stdin傳遞內容,使用echo管道。我應該如何正確地逃避這個字符串?

我認爲字符串中有一些字符會中斷命令,但我無法弄清楚哪個字符。我在Ubuntu 14.04上運行PHP 5.6,在這種情況下是CLI。

您可以運行此看到自己(需要Ruby和薩斯安裝):

sudo易於得到安裝Ruby & & sudo的創業板安裝青菜

<?php 

/** Should work with '$large = true' and '$download = false' **/ 

// to prove that a small file DOES compile via stdin 
$large = true; 

// to prove that it's compilable as a file, rather than stdin 
$download = false; 

$domain = "http://test.fhmp.net"; 
$file = $large ? 'large' : 'small'; 

// grab a valid .scss file 
$input = file_get_contents("$domain/$file.scss"); 

if($download){ 

    // create temp file 
    $temp = tempnam(sys_get_temp_dir(), ''); 
    file_put_contents($temp, $input); 

    // compile temp file 
    var_dump(shell_exec("sass --scss '$temp' 2>&1")); 

    // delete temp file 
    @unlink($temp); 

} else { 

    // attempt to escape it 
    $esc = escapeshellarg($input); 

    // dump the results of the shell call 
    var_dump(shell_exec("echo $esc | sass --scss --stdin 2>&1")); 
} 
+1

你應該在這裏發佈錯誤,以及它發生的地方。 – Brad 2014-09-20 02:45:07

+0

對不起可能是愚蠢的評論,但對於三元組,你不需要一個比較運算符爲你在這裏嘗試做什麼? - > $ file = $ large? '大小'; //應該是$ file === $ large? '大小'; //對? – 2014-09-20 02:47:10

+0

@TheOneandOnlyChemistryBlob,我實際上將一個字符串賦值給'$ file',根據$ large的真實性,這個字符串可以是'large'或'small'。 – rtheunissen 2014-09-20 02:48:46

回答

0

我認爲這裏的問題是,當我插入整個輸入字符串時,該命令太長。解決方法是使用proc_openpopen寫入標準輸入而不是管道使用|

0

你應該嘗試:

$ esc ='$'。escapeshellarg($ input);

escapeshellarg()函數用反斜槓轉義單引號,這意味着: escapeshellarg("//where's the quote")變成'where\'s the quote'。你不能在(bash)shell中的單引號內回顯單引號,請參閱:How to escape a single quote in single quote string in Bash?

+0

我已經在使用'escapeshellarg'。 – rtheunissen 2014-11-02 23:09:09

+0

將美元符號放在escapeshellarg結果之前並將其轉儲到shell_exec中 – 2014-11-02 23:13:53

相關問題