php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67361 copy() fails on filename with newline
Submitted: 2014-05-30 06:46 UTC Modified: 2014-06-19 10:44 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: tux at krageroby dot no Assigned:
Status: Closed Package: Filesystem function related
PHP Version: 5.5.13 OS: xUbuntu
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: tux at krageroby dot no
New email:
PHP Version: OS:

 

 [2014-05-30 06:46 UTC] tux at krageroby dot no
Description:
------------
When trying to use php copy() function on a file with a newline character in the filename, it fails to open stream.

Test script:
---------------
php -r 'copy("filename_with\r\nNewline_character", "/tmp/copytest");'

Actual result:
--------------
php -r 'copy("filename_with\r\nNewline_character", "/tmp/copytest");'
PHP Warning:  copy(filename_with
Newline_character): failed to open stream: No such file or directory in Command line code on line 1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-06-18 01:21 UTC] husman85 at gmail dot com
The reason why this fails is because you have escape sequences wrapped in double quotes. If you try the same thing with single quotes, then you will notice this function is working properly. Escape sequences are parsed in the context of double quotes.
 [2014-06-18 08:23 UTC] tux at krageroby dot no
Yes, this bug occurs when the filename contains a real windows newline in the filename. so, if using single quotes, the result is file not found. The bug in my case occurs when unpacking files from a .zip file that has a windows newline in the filename.
 [2014-06-18 14:59 UTC] Husman85 at gmail dot com
This is not a bug. See below:

Test script:
--------------
<?php

// Create a directory to work with
mkdir("testdir", 0777);

// Create a file with Windows-style newline in the file name.
file_put_contents("testdir/filename_with\r\nNewline_character", "test 321");

// Read active directory
$files = scandir('testdir');

// print the directories/files
print_r($files);

// Store the file name of the created file
$created_file = $files[2];

// Attempt to copy file:
echo "\n";
copy("testdir/filename_with\r\nNewline_character", "testdir/copytest");
echo "\n";

// Output files in testdir
$files = scandir('testdir');
print_r($files);

// Can we find "\r\n" in the file name?
echo "\n";
$escaped_name = str_replace(array("\r\n"), '\r\n', $created_file);
echo "created/copied filename: ". $escaped_name;
echo "\n";

?>

Expected result (if bug):
-------------------------
Array
(
    [0] => .
    [1] => ..
    [2] => filename_with
Newline_character
)

Warning: copy(testdir/filename_with
Newline_character): failed to open stream: No such file or directory in /home/husman/Desktop/test/php/bug_67361.php on line 20

Array
(
    [0] => .
    [1] => ..
    [2] => copytest
    [3] => filename_with
Newline_character
)

created/copied filename: filename_with\r\nNewline_character


Actual result:
-------------------------
Array
(
    [0] => .
    [1] => ..
    [2] => filename_with
Newline_character
)

Array
(
    [0] => .
    [1] => ..
    [2] => copytest
    [3] => filename_with
Newline_character
)

created/copied filename: filename_with\r\nNewline_character


Further Comments:
-----------------
Maybe the file name is not being properly encoded when unpacked from the zip file?
 [2014-06-18 15:02 UTC] Husman85 at gmail dot com
Typo in "Expected results (if bug)" in the previous post. It should be:

Expected result (if bug):
-------------------------
Array
(
    [0] => .
    [1] => ..
    [2] => filename_with
Newline_character
)

Warning: copy(testdir/filename_with
Newline_character): failed to open stream: No such file or directory in /home/husman/Desktop/test/php/bug_67361.php on line 20

Array
(
    [0] => .
    [1] => ..
    [2] => filename_with
Newline_character
)

created/copied filename: filename_with\r\nNewline_character
 [2014-06-19 10:43 UTC] tux at krageroby dot no
[not a bug]

You are completely right, seems like it was the zip extraction that converted the windows newlines to a unix newline, and therefore the filename was changed before the copy command ran with the original name. I feel embarrassed now, sorry to have been yet another not too good tested bug report. I will file the bug to the zip extractors instead.
 [2014-06-19 10:44 UTC] tux at krageroby dot no
-Status: Open +Status: Closed
 [2014-06-19 10:44 UTC] tux at krageroby dot no
Not a bug, see comments above.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Fri Dec 09 03:03:42 2022 UTC