note 102274 added to function.rename

From: Date: Sat, 05 Feb 2011 18:01:17 +0000
Subject: note 102274 added to function.rename
Groups: php.notes 
Request: Send a blank email to php-notes+get-176703@lists.php.net to get a copy of this message
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

« previous php.notes (#176703) next »