Bug #71630 [Opn->Fbk]: copy function does not work if destination file does not exist
Edit report at https://bugs.php.net/bug.php?id=71630&edit=1
ID: 71630
Updated by: danack@php.net
Reported by: vladimir at volomp dot com
Summary: copy function does not work if destination file does
not exist
-Status: Open
+Status: Feedback
Type: Bug
Package: *General Issues
Operating System: linux/all
PHP Version: 5.5.32
Block user comment: N
Private report: N
New Comment:
Please can you provide some context of how you are encountering a problem. i.e. provide a reproduce
scripts.
The code below works for me:
<?php
file_put_contents("a.txt", "These are some words");
copy("a.txt", "b.txt");
var_dump(file_exists("b.txt"));
//output is bool(true)
without b.txt existing first.
Previous Comments:
------------------------------------------------------------------------
[2016-02-19 12:39:32] vladimir at volomp dot com
Description:
------------
Fails to copy a.txt to b.txt if b.txt is missing.
If we just touch b.txt, copy will work.
There is possibly relevant code in ext/standard/file.c in function php_copy_file_ctx() where dest is
checked the same way source is (suspected copy&paste).
switch (php_stream_stat_path_ex(dest, PHP_STREAM_URL_STAT_QUIET |
PHP_STREAM_URL_STAT_NOCACHE, &dest_s, ctx)) {
case -1:
/* non-statable stream */
goto safe_to_copy;
break;
case 0:
break;
default: /* failed to stat file, does not exist? */
return ret;
}
So it is unclear if PHP_STREAM_URL_STAT_QUIET has any influence on return code - but I guess NO, so
then this block of code is incorrect and should have default goto safe_to_copy.
This code is more-less same for php 5.6 and 7, however 5.2.17 which we still use do not have this
bug as check is done differently:
if (php_stream_stat_path_ex(dest, PHP_STREAM_URL_STAT_QUIET, &dest_s, NULL) != 0) {
goto safe_to_copy;
}
Test script:
---------------
copy( "a.txt", "b.txt" );
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=71630&edit=1
Thread (9 messages)