php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44805 rename() function is not portable to Windows
Submitted: 2008-04-23 06:53 UTC Modified: 2008-04-24 07:53 UTC
From: rasmus at mindplay dot dk Assigned: dmitry (profile)
Status: Closed Package: Filesystem function related
PHP Version: 5.2.5 OS: WindowsXP/DebianLinux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rasmus at mindplay dot dk
New email:
PHP Version: OS:

 

 [2008-04-23 06:53 UTC] rasmus at mindplay dot dk
Description:
------------
The behavior of rename() is inconsistent between Linux and Windows.

On Linux, the rename() function is atomic - you can rename() one file over an existing file, and the replacement is atomic.

On Windows, you cannot rename over an existing file.

Reproduce code:
---------------
<?php

file_put_contents("file1.txt", "this is file 1");
file_put_contents("file2.txt", "this is file 2");

rename("file1.txt", "file2.txt");

echo "reading file 2: ";
readfile("file2.txt");

?>

Expected result:
----------------
reading file 2: this is file 1


Actual result:
--------------
On Linux: (CORRECT)

reading file 2: this is file 1

On Windows: (INCORRECT)

Warning: rename(file1.txt,file2.txt) [function.rename]: File exists in C:\Web3\test\rename\test.php on line 6
reading file 2: this is file 2


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-23 06:57 UTC] rasmus at mindplay dot dk
File rename with overwrite code for Windows here:

http://rikkus.info/rename-with-overwrite-on-windows.html
 [2008-04-23 07:36 UTC] pajoye@php.net
That's correct. We already uses MoveFileEx in ext/zip (prooving that it  is well supported by the current php windows setup).

Patch + tests will come shortly.
 [2008-04-23 14:21 UTC] pajoye@php.net
For the record here:

http://blog.thepimp.net/patches/TSRM_windows_rename.patch.txt
http://blog.thepimp.net/patches/bug44805.phpt

It fixes this issue. Dmitry will commit it in the next couple of days.

Assigned to Dmitry.
 [2008-04-24 07:53 UTC] dmitry@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu May 08 00:01:29 2025 UTC