名前にスペースが含まれるファイルが既にサーバー上にあります。アンダースコアで名前を変更したい。スペースの前にスラッシュを入れてみました。スペースを %20 に置き換えてみました。それでもPHPは私に与えます:
そのようなファイル、又はディレクトリはありません。
これが私のコードです:
$dir = './';
$dhandle = opendir($dir);
$old_files = array();
if ($dhandle) {
while (false !== ($fname = readdir($dhandle)))
{
if ( ($fname != '.') && ($fname != '..') )
{
$old_files[] = (is_dir( "./$fname" )) ? "(Dir) {$fname}" : $fname;
}
}
closedir($dhandle);
}
$new_files = str_replace(' ', '_', $old_files);
$x = 0;
foreach($old_files as $file)
{
rename(rawurlencode($file), $new_files[$x++]);
}