rename() is working on Linux/UNIX but not working on Windows on a directory containing a file formerly opened within the same script. The problem persists even after properly closing the file and flushing the buffer.
<?php
$fp = fopen ("./dir/ex.txt" , "r+");
$text = fread($fp, filesize("../dir/ex.txt"));
ftruncate($fp, 0);
$text2 = "some value";
fwrite ($fp, $text2 );
fflush($fp);
fclose ($fp);
if (is_resource($fp))
fclose($fp);
rename ("./dir", ".dir2"); // will give a «permission denied» error
?>
Strangely it seem that the rename command is executed BEFORE the handle closing on Windows.
Inserting a sleep() command before the renaming cures the problem.
<?php
$fp = fopen ("./dir/ex.txt" , "r+");
$text = fread($fp, filesize("../dir/ex.txt"));
ftruncate($fp, 0);
$text2 = "some value";
fwrite ($fp, $text2 );
fflush($fp);
fclose ($fp);
if (is_resource($fp))
fclose($fp);
sleep(1); // this does the trick
rename ("./dir", ".dir2"); //no error
?>