How should I be serving binary files (images / audio) from a RESTful api written in PHP? How should I be serving binary files (images / audio) from a RESTful api written in PHP? json json

How should I be serving binary files (images / audio) from a RESTful api written in PHP?


The world most used APIs like Facebook, Flickr and Instagram provide static links to the images.

If you provide your binary files encoded in base64 they will be heavier and it will require more CPU resources both on the server side and on the client side to encode/decode them.

The only case where there are benefits to bundle the files is if you want to send many very small files like icons.


Generally, it is a (really) bad idea to use an ASCII based protocol to transfer binary files, especially when they are heavy. You could expose an URI where the consumer will be able to download the file without using the REST API, and use a separate PHP script (or any other kind of language) to perform the access control part using sessions and output true binary data with the required HTTP Headers. The last part is pretty straightforward.

http://fr2.php.net/manual/en/function.fpassthru.php explains how to output binary data without hassle.

Hope it helps.