php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #41985 rename doesn't work
Submitted: 2007-07-12 22:26 UTC Modified: 2013-03-20 20:13 UTC
Votes:6
Avg. Score:4.3 ± 0.9
Reproduced:5 of 5 (100.0%)
Same Version:4 (80.0%)
Same OS:5 (100.0%)
From: marc dot bau at gmx dot net Assigned: pajoye (profile)
Status: Closed Package: *General Issues
PHP Version: 5.2.3 OS: Windows
Private report: No CVE-ID: None
 [2007-07-12 22:26 UTC] marc dot bau at gmx dot net
Description:
------------
The "rename" in PHP doesn't work as expected on Windows.

Reproduce code:
---------------
<?php
rename("C:\temp\tmp_file.txt.new", "C:\temp\tmp_file.txt");
?>

Expected result:
----------------
successful rename completed.

Actual result:
--------------
1. rename isn't working
2. C:\temp\tmp_file.txt is not replaced with C:\temp\tmp_file.txt.new
3. C:\temp\tmp_file.txt.new not moved to C:\temp\tmp_file.txt

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-12 22:50 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

\\t has a special meaning in strings with double quotes.
 [2007-07-13 06:12 UTC] marc dot bau at gmx dot net
What are you talking about? Writing write code once, run everywhere? This is currently not possible and therefor this is for sure a bug.

Rename is not working the same way on all OS'es and this is why i filed this bug. I don't like to workaround in a shit way like described in http://us.php.net/manual/en/function.rename.php#56576. This one produces a PHP error that is logged in my CMS.

So let's fix this to make PHP work in the same way on all OS without dirty hacks. You may fix this in the way of #56576 PHP *internally*, but don't make me to hack around outside and create crappy code around.
 [2007-07-13 18:33 UTC] marc dot bau at gmx dot net
This doesn't have anything to do with double quotes. With single quotes this isn't working, too.

<?php
rename('C:\temp\tmp_file.txt.new', 'C:\temp\tmp_file.txt');
?>
 [2007-07-13 18:44 UTC] tony2001@php.net
Please escape the \ as advised in the manual.
 [2007-07-13 20:36 UTC] marc dot bau at gmx dot net
As already said, this won't help anything.

DON'T WORK:
<?php
rename('C:\temp\tmp_file.txt.new', 'C:\temp\tmp_file.txt');
?>

Error:
PHP Warning:  rename(C:\temp\tmp_file.txt.new,C:\temp\tmp_file.txt): File exists in C:\Temp\rename-test.php on line 2

DON'T WORK:
<?php
rename('C:\\temp\\tmp_file.txt.new', 'C:\\temp\\tmp_file.txt');
?>

Error:
PHP Warning:  rename(C:\temp\tmp_file.txt.new,C:\temp\tmp_file.txt): File exists in C:\Temp\rename-test.php on line 2

DON'T WORK:
<?php
rename("C:\\temp\\tmp_file.txt.new", "C:\\temp\\tmp_file.txt");
?>

Error:
PHP Warning:  rename(C:\temp\tmp_file.txt.new,C:\temp\tmp_file.txt): File exists in C:\Temp\rename-test.php on line 2

DON'T WORK:
<?php
rename("C:/temp/tmp_file.txt.new", "C:/temp/tmp_file.txt");
?>

Error:
PHP Warning:  rename(C:/temp/tmp_file.txt.new,C:/temp/tmp_file.txt): File exists in C:\Temp\rename-test.php on line 2


So you have a Windows Box next to you? Before answering, test yourself if you don't trust me, but don't close this case until this bug has been fixed! THX.
 [2007-07-13 20:48 UTC] tony2001@php.net
Please open a dictionary and translate the error message:
PHP Warning:  rename(C:\temp\tmp_file.txt.new,C:\temp\tmp_file.txt):
****File exists**** in C:\Temp\rename-test.php on line 2

It says FILE EXISTS, don't you see it?
Please do NOT reopen the report again.
Thank you.
 [2007-07-13 20:57 UTC] marc dot bau at gmx dot net
Do you know how this works on *nix systems?

Well i will tell you - it will *move* the "new file" over the "old file" in an atomic rename. This is what PHP needs to handle on Windows, too. I don't need any extras on *nix to rename. And PHP internal command should work platform independent. 

This bug should be fixed to make sure the code runs on windows in the *same way* as it does on *nix systems.

What is so difficult to understand about this? Writing write code once, *run everywhere* is not possible with this bug.
 [2007-07-14 10:35 UTC] derick@php.net
I actually agree with that... however, it's not possible to do this atomically on windows if the file you're moving to exists. The only option is to unlink() the target and then do the rename() but that leaves a race condition. I don't think that is solvable on Windows.
 [2007-07-14 12:58 UTC] marc dot bau at gmx dot net
Well, if this rename is not possible atomically (how bad this ever is), i think PHP should handle the copy/unlink logic in background if i use "rename()".

I simply don't like to think about possible platform dependencies (i don't know anything about) and hack around in a bad way that additional produce an error with "E_ALL & ~E_NOTICE" i cannot circumvent.
 [2009-12-08 06:10 UTC] rainmanhhh at tom dot com
rename should be remake to copy + unlink to keep php do the same behavior on windows & *nix
 [2013-03-20 19:34 UTC] cmbecker69 at gmx dot de
This issue is solved since PHP 5.3.0 (released 2009-06-30). 
rename() works atomic on Windows due to the use of 
MoveFileEx(..., ..., MOVEFILE_REPLACE_EXISTING) 
(see virtual_rename() in TSRM/tsrm_virtual_cwd.c 
resp. VCWD_RENAME() in TSRM/tsrm_virtual.cwd.h).

So this issue can be closed.
 [2013-03-20 20:13 UTC] pajoye@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: pajoye
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Wed Aug 21 04:01:26 2019 UTC