note 128626 added to function.header

From: Date: Tue, 20 Jun 2023 13:44:03 +0000
Subject: note 128626 added to function.header
Groups: php.notes 
Request: Send a blank email to php-notes+get-220585@lists.php.net to get a copy of this message
If you use header() to allow the user to download a file, it's very important to check the encoding of the script itself. Your script should be encoded in UTF-8, but definitely not in UTF-8-BOM! The presence of BOM will alter the file received by the user. Let the following script: <?php $content = file_get_contents('test_download.png') ; $name = 'test.png' ; $size = strlen($content) ; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Cache-Control: no-cache, must-revalidate'); header('Expires: 0'); header('Content-Disposition: attachment; filename="'.$name.'"'); header('Content-Length: ' . $size); header('Pragma: public'); echo $content ; ?> Irrespectively from the encoding of test_download.png, when this PHP script is encoded in UTF-8-BOM, the content received by the user is different: - a ZWNBSP byte (U+FEFF) is added to the beginning of the file - the file content is truncated!!! If it's a binary file (e.g. image, proprietary format), the file will become unreadable. ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.13.130 (proxied: 194.254.118.32) ---- Manual Page -- https://php.net/manual/en/function.header.php Edit -- https://main.php.net/note/edit/128626 Del: integrated -- https://main.php.net/note/delete/128626/integrated Del: useless -- https://main.php.net/note/delete/128626/useless Del: bad code -- https://main.php.net/note/delete/128626/bad+code Del: spam -- https://main.php.net/note/delete/128626/spam Del: non-english -- https://main.php.net/note/delete/128626/non-english Del: in docs -- https://main.php.net/note/delete/128626/in+docs Del: other reasons-- https://main.php.net/note/delete/128626 Reject -- https://main.php.net/note/reject/128626 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220585) next »