PHP 8.5.0 Alpha 2 available for testing

Voting

: max(two, one)?
(Example: nine)

The Note You're Voting On

Martin Pelletier
14 years ago
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
?>

<< Back to user notes page

To Top