Incorrect encoding when reading from binary file using PHP

The problem:

I am trying to read a binary file from my web server via PHP but it’s coming back with an incorrect encoding (I think, though I can’t be sure).

Why I am doing this is because I want to remove the need for the user to download a file and populate an <input>, instead I want to automate the process so the file is automatically retrieved via PHP and the contents are processed by the clientside JS. I know this seems counter-intuitive, but it’s a very specific use-case for flashing ESP chips from ESPTool-JS.

Example results:

The reason I think this is an encoding issue is because this is what I get when reading from a localfile (contents truncated):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>x01x00x00x00ðx0Fÿ÷littlefs/
</code>
<code>x01x00x00x00ðx0Fÿ÷littlefs/ </code>
x01x00x00x00ðx0Fÿ÷littlefs/

And this is what I get when reading the same file from the server:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>x01x00x00x00�x0F��littlefs/
</code>
<code>x01x00x00x00�x0F��littlefs/ </code>
x01x00x00x00�x0F��littlefs/

My PHP Code

When running locally I am using an <input type="file" /> element and when running on the server I am using this PHP code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$filePath = "bins/" . $_REQUEST["file"];
if (file_exists($filePath)) {
$fileContents = file_get_contents($filePath);
if ($fileContents === false) {
http_response_code(500);
die('Failed to read file');
}
// Remove BOM if present
$fileContents = preg_replace('/^xEFxBBxBF/', '', $fileContents);
// Set appropriate headers
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($filePath).'"');
// Output the file contents
echo $fileContents;
} else {
http_response_code(404);
die('File not found');
}
</code>
<code>$filePath = "bins/" . $_REQUEST["file"]; if (file_exists($filePath)) { $fileContents = file_get_contents($filePath); if ($fileContents === false) { http_response_code(500); die('Failed to read file'); } // Remove BOM if present $fileContents = preg_replace('/^xEFxBBxBF/', '', $fileContents); // Set appropriate headers header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="'.basename($filePath).'"'); // Output the file contents echo $fileContents; } else { http_response_code(404); die('File not found'); } </code>
$filePath = "bins/" . $_REQUEST["file"];

if (file_exists($filePath)) {
    $fileContents = file_get_contents($filePath);

    if ($fileContents === false) {
        http_response_code(500);
        die('Failed to read file');
    }

    // Remove BOM if present
    $fileContents = preg_replace('/^xEFxBBxBF/', '', $fileContents);

    // Set appropriate headers
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="'.basename($filePath).'"');

    // Output the file contents
    echo $fileContents;
} else {
    http_response_code(404);
    die('File not found');
}

My PHP.ini:

Please note that my PHP.ini file is set to UTF-8 for both the default_charset and the internal_encoding settings.

I’ve been battling this for a long time now! Help, pretty please!! ^_^

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật