いくつかの単純な文字列照合を行うだけでよいので、正規表現は実際にはこれにはやり過ぎです。
$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);
}
}
次に、質問について:
または、すべてのファイル(index.phpを含む)を最初に/ tmp /に移動し、後でindex.phpのみを/public_html/に戻すのはどうでしょうか。CPUの消費量が少ないと思います。
それは可能であり、おそらくあなたのCPUでは少し簡単でしょう。ただし、これが問題にならない理由はいくつかあります。まず、PHPを介してこれを実行することにより、すでに非常に非効率的な方法でこれを実行しているため、PHPの外部で実行する意思がない限り、この時点でCPUにかかる負担を実際に確認するべきではありません。第二に、それはより多くのディスクアクセスを引き起こし(特にソースとデスティネーションディレクトリが同じディスクまたはパーティション上にない場合)、ディスクアクセスはCPUよりもはるかに遅くなります。