note 102274 added to function.rename

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
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 
?>
----
Server IP: 69.147.83.197
Probable Submitter: 207.96.205.12
----
Manual Page -- http://www.php.net/manual/en/function.rename.php
Edit        -- https://master.php.net/note/edit/102274
Del: integrated  -- https://master.php.net/note/delete/102274/integrated
Del: useless     -- https://master.php.net/note/delete/102274/useless
Del: bad code    -- https://master.php.net/note/delete/102274/bad+code
Del: spam        -- https://master.php.net/note/delete/102274/spam
Del: non-english -- https://master.php.net/note/delete/102274/non-english
Del: in docs     -- https://master.php.net/note/delete/102274/in+docs
Del: other reasons-- https://master.php.net/note/delete/102274
Reject      -- https://master.php.net/note/reject/102274
Search      -- https://master.php.net/manage/user-notes.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.