|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-09-13 16:15 UTC] christian at vaneeden dot ca
Description:
------------
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.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 02 13:00:01 2025 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: <?php error_reporting(E_ALL); 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..