|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81145 copy() and stream_copy_to_stream() fail for +4GB files
Submitted: 2021-06-16 09:43 UTC Modified: 2022-08-04 08:39 UTC
From: php at bitm dot sg Assigned: cmb (profile)
Status: Closed Package: Filesystem function related
PHP Version: 7.4 OS: Windows 10.0.19042.985 x64
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.
Block user comment
Status: Assign to:
Bug Type:
From: php at bitm dot sg
New email:
PHP Version: OS:


 [2021-06-16 09:43 UTC] php at bitm dot sg
Using copy() or stream_copy_to_stream() fails on Windows with files over 4GB, even if the underlying volume (NTFS) supports it and Windows + PHP are 64 bit.

Test script:
	//Both files must be on an NTFS volume. You may use the same volume.

	//Create file and append random content at the 4GB boundary
	exec('fsutil file createnew ' . escapeshellarg($src) . ' ' . (SIZE_4G-0x100));
	if(filesize($src)!==filesize($dst)){die('Files have different sizes!');}
	$f1=fopen($src,'rb') or die('src open failed');
	$f2=fopen($dst,'rb') or die('dst open failed');
	//Seek to 4 GB boundary, as this is the location where the problem occurs
	echo (fread($f1,0x200)===fread($f2,0x200)?'Identical':'Copy failed');

Expected result:
The functions should copy files and streams as-is and the script should print "Identical"

Actual result:
Script prints "Copy failed"

Only the first 4 GB of the source file are copied.
This is actually done repeatedly until the destination is as large as the source. So if the file is 13 GB, the destination will contain the first 4 GB of the source 3 times, followed by the first GB again.

using "fwrite($b,fread($a,$buffer))" to manually copy files works.


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-06-16 09:56 UTC]
-Assigned To: +Assigned To: cmb
 [2021-06-16 09:56 UTC]
Seems to work fine on Linux.
 [2021-06-16 10:14 UTC] php at bitm dot sg
-Operating System: Microsoft Windows [Version 1 x64 +Operating System: Windows 10.0.19042.985 x64
 [2021-06-16 10:14 UTC] php at bitm dot sg
Fixed the Windows version because it was cut off.
 [2021-06-16 11:04 UTC]
-Status: Assigned +Status: Analyzed -PHP Version: 8.0.7 +PHP Version: 7.4
 [2021-06-16 11:04 UTC]
Confirmed (affects PHP-7.4 on Windows as well).

Thanks for the helpful analysis on reddit[1].

[1] <>
 [2021-06-16 11:19 UTC]
The following pull request has been associated:

Patch Name: Fix #81145: copy() and stream_copy_to_stream() fail for +4GB files
On GitHub:
 [2021-06-17 11:19 UTC]
Automatic comment on behalf of cmb69
Log: Fix #81145: copy() and stream_copy_to_stream() fail for +4GB files
 [2021-06-17 11:19 UTC]
-Status: Analyzed +Status: Closed
 [2022-08-04 00:14 UTC] ken at nowsoftwaresolutions dot com
I received this bug after building PHP 8.1.8 on a Raspberry Pi Model 4B with 8 GB RAM running Ubuntu Server 20.04.4 LTS.
 [2022-08-04 08:39 UTC]
Ken, please file a new ticket at
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Mon May 29 06:03:41 2023 UTC