note 88226 rejected from function.copy by thiago
| From: | thiago@php.net | Date: | Fri, 16 Jan 2009 12:10:05 +0000 |
| Subject: | note 88226 rejected from function.copy by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-149313@lists.php.net to get a copy of this message | ||
Note Submitter: a dot u dot savchuk at gmail dot com
----
Function copy() has a bug (or strange behavior).
When you copy file to _already_ existed file-target,
then info of this file-target will be cached before rewrite.
So, for example, result of code:
------------------
<?php
$source = '/t1.t';
$target = '/t2.t';
file_put_contents($source, 'aaaaaaaaaa');
file_put_contents($target, 'a');
clearstatcache( true );
copy( $source, $target );
var_dump( filesize($target) );
------------------
will be:
------------------
int(1)
------------------
instead of expected:
------------------
int(10)
------------------
Workaround here is usage of clearstatcache().
Also other problem is that copy() function is not listed in list of cached functions in manual for
clearstatcache().
I hope this info will help.
P.S.: as i understand from source code, the problem is that copy() uses php_stream_stat_path_ex() [
_php_stream_stat_path() ] function which stores stat info in cache. Correct solutions are:
1. add parameter to php_stream_stat_path_ex() to not store info to cache and call it for destination
path in php_copy_file_ex()
2. clear cache after successfull call of php_stream_stat_path_ex() for destination path in
php_copy_file_ex().