简体   繁体   中英

Error Serving WOFF File in PHP - file_get_contents versus readfile

I am trying to serve a WOFF font file in PHP.

First I set the Content-Type header:

header("Content-Type: application/font-woff");

Then I try the following:

This works fine (the file is properly served to the user, and the Content-Length header matches the file size):

readfile("path/to/font.woff");

But this doesn't (the script completes without error, but I receive an "octet stream" error in the browser, and the Content-Length header is 0):

echo file_get_contents("path/to/font.woff");

Why am I receiving an error using file_get_contents? I am using Windows 10, IIS Express 10, and PHP 7.2

You should check your server logs and your log_level. file_get_contents returns false if there was an error reading the file. I've tested it right now and echo(false) returns an empty string. In this case the response body would be empty (and the content length be 0).

The reason, that file_get_content is failing could either be an incorrect path or, that the file content cannot be interpreted as an string. File_get_contents tries to create a string out of the file. It could be, that this does not work with a woff file. But I'm not shure on this. You should check the logs (E_WARNING log level).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM