我試圖用shell_exec
或exec
,而不是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"));
}
你應該在這裏發佈錯誤,以及它發生的地方。 – Brad 2014-09-20 02:45:07
對不起可能是愚蠢的評論,但對於三元組,你不需要一個比較運算符爲你在這裏嘗試做什麼? - > $ file = $ large? '大小'; //應該是$ file === $ large? '大小'; //對? – 2014-09-20 02:47:10
@TheOneandOnlyChemistryBlob,我實際上將一個字符串賦值給'$ file',根據$ large的真實性,這個字符串可以是'large'或'small'。 – rtheunissen 2014-09-20 02:48:46