rsyncの--excludeオプションがphpスクリプトの中で効かない。どうもsystem関数の動きがおかしそう。で、こんなスクリプトを作ってみた。
<?php error_reporting(E_ALL); $str = "--exclude={'*.wav','*.mp3','log/','*.img','log/'}"; system("echo $str\n"); passthru("echo $str\n"); echo (exec("echo $str\n"));echo "\n"; echo `echo $str`; echo "$str\n"; ?>
やはりrsyncの--excludeがうまくいかないのはここか。外部プログラムを実行する際にはコーテーションが取り除かれると。シングルコーテーションとダブルコーテーションを入れ替えてもやはりコーテーションが取り除かれる。
一部エスケープしてみた。
<?php error_reporting(E_ALL); $str = "--exclude={\'*.wav\','*.mp3','*.log','*.img','log/'}"; system("echo $str\n"); passthru("echo $str\n"); echo (exec("echo $str\n"));echo "\n"; echo `echo $str`; echo "$str\n"; ?>
とりあえずエスケープしたら通りそう。
replaceしてみる。
<?php error_reporting(E_ALL); $str = str_replace("'","\'","--exclude={'*.wav','*.mp3','*.log','*.img','log/'}"); system("echo $str\n"); passthru("echo $str\n"); echo (exec("echo $str\n"));echo "\n"; echo `echo $str`; echo "$str\n"; ?>
これでええか。
でもなんでこんな仕様なんやろ?セキュリティ対策?とりあえずescapeshellargという関数があることにたどり着いた。
<?php error_reporting(E_ALL); $str = escapeshellarg("--exclude={'*.wav','*.mp3','*.log','*.img','log/'}"); system("echo $str\n"); passthru("echo $str\n"); echo (exec("echo $str\n"));echo "\n"; echo `echo $str`; echo "$str\n"; ?>
実行結果。