php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71035 Rename() on Windows fails with read-only permissions on the target file
Submitted: 2015-12-04 22:49 UTC Modified: 2015-12-09 19:39 UTC
From: info at mikekeran dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.5.30 OS: Windows
Private report: No CVE-ID: None
 [2015-12-04 22:49 UTC] info at mikekeran dot com
Description:
------------
When using rename to replace an existing file, PHP on Windows incorrectly gives an access denied if the target file is read-only (0444):

An access denied error should only be given if the target file's directory permissions should keep the current user from writing to that location. Linux (tested on Ubuntu 14.04) works as expected.

Test script:
---------------
<?php
file_put_contents('./test.txt', 'test data');
file_put_contents('./newtest.txt', 'new test data');
chmod('test.txt', 0666);
rename('newtest.txt', 'test.txt');
print "test.txt is now: " . file_get_contents('./test.txt') . "\n";

file_put_contents('./test.txt', 'test data');
file_put_contents('./newtest.txt', 'new test data');
chmod('test.txt', 0444);
rename('newtest.txt', 'test.txt');

print "test.txt is now: " . file_get_contents('./test.txt') . "\n";



Expected result:
----------------
test.txt is now: new test data
test.txt is now: new test data


Actual result:
--------------
test.txt is now: new test data

Warning:  rename(newtest.txt,test.txt): Access is denied. (code: 5) in C:\Users\Mike\Documents\dev\sandbox\sandbox.php on line 13

test.txt is now: test data


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-12-05 01:48 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-12-05 01:48 UTC] requinix@php.net
Windows permission system (including the read-only attribute) does not work like Unix's permission system. Read-only prevents changes to the file, and that includes replacing the file with something else.

Try it yourself without PHP: make a file, mark it read-only, then try to copy another file onto it.
 [2015-12-09 19:02 UTC] info at mikekeran dot com
Thank you for the quick reply, requinix@php.net.

I understand the differences in the underlying file permissions, however I expected PHP to normalize that. Couldn't rename() chmod 0666 the file before overwriting it? Yes, I realize that I can do that in my script, but it seems to me that this level of abstraction should happen at the language level.

Thoughts?
 [2015-12-09 19:30 UTC] requinix@php.net
Normalizing could make sense if there was an accepted standard for how it should work. But there isn't.
There are many things that differ across platforms and that a developer should be aware of. This is one of them.
 [2015-12-09 19:39 UTC] info at mikekeran dot com
Fair enough. I'll file a bug to update the docs page.

Thanks.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 18 17:01:28 2024 UTC