|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42661 file_get_contents() reads binary data incorrectly
Submitted: 2007-09-13 16:15 UTC Modified: 2007-09-27 01:00 UTC
Avg. Score:4.4 ± 0.7
Reproduced:8 of 8 (100.0%)
Same Version:1 (12.5%)
Same OS:1 (12.5%)
From: christian at vaneeden dot ca Assigned:
Status: No Feedback Package: Streams related
PHP Version: 5.2.4 OS: Redhat AS 4
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
48 + 42 = ?
Subscribe to this entry?

 [2007-09-13 16:15 UTC] christian at vaneeden dot ca
When you read in a binary file with file_get_contents() and try to output it, it comes out garbled.
Note: as a workaround use readfile()

Reproduce code:
$docFile = file_get_contents( 'word.doc' );
header('Content-type: application/msword; charset: UTF-8');
header('Content-Disposition: attachment; filename="word.doc'"');
header('Cache-Control: must-revalidate');
print $docFile;

Expected result:
A proper word document.

Actual result:
Garbled binary text.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-09-19 10:30 UTC]
Just a note: using file_get_contents() in this is not very useful, readfile() is much better anyway. 

Try this little script to see that it's is actually file_get_contents() that is buggy:

ini_set('display_errors', 1);
$docFile = file_get_contents( 'word.doc' );
echo strlen($docFile), "\n";
echo file_put_contents( 'copy_word.doc', $docFile ), "\n";

Then run this on the result:

# diff -a word.doc copy_word.doc

They should not differ..
 [2007-09-27 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2009-11-02 01:22 UTC] alex94040 at yahoo dot com
I'm experiencing the same issue on PHP 5.2.9 on Apache + Windows 7. readfile() as a workaround doesn't work for me - same garbled text.
 [2012-07-02 17:18 UTC] phare2013 at gmail dot com
"readfile()" worked for me once,after that it also returned garbled binary.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon May 27 09:01:30 2024 UTC