的正則表達式其實矯枉過正這一點,因爲我們只需要做一些簡單的字符串匹配:對於這個問題
$dir = 'the_directory/';
$handle = opendir($dir) or die("Problem opening the directory");
while ($filename = readdir($handle) !== false)
{
//if ($filename != 'index.php' && substr($filename, -3) == '.php')
// I originally thought you only wanted to move php files, but upon
// rereading I think it's not what you really want
// If you don't want to move non-php files, use the line above,
// otherwise the line below
if ($filename != 'index.php')
{
rename($dir . $filename, '/tmp/' . $filename);
}
}
然後:
alternatively, how about moving all files (including index.php) first to /tmp/ then later on put only index.php back to /public_html/, which one you think is less CPU consuming?
這是可以做到,它會可能在你的CPU上稍微容易一些。然而,這並不重要的原因有幾個。首先,你已經以非常低效的方式通過PHP來實現這個目標,所以你不應該在這個時候考慮這會給你的CPU帶來的壓力,除非你願意在PHP之外這麼做。其次,這會導致更多的磁盤訪問(特別是如果源目錄和目標目錄不在同一磁盤或分區中),並且磁盤訪問比CPU慢得多。
你是什麼BASH問題是什麼意思?我不明白... –
你只有使用PHP才能移動它嗎?如果那是一次,你可以使用shell,比如bash。 – anishsane
由於我的代碼是PHP,我確定我想使用PHP來移動它。 –