cpordoegr Posted May 9, 2013 Share Posted May 9, 2013 Hi, I need some help in sending files as a response to a POST request. I have written a PHP RESt API which is already receiving files with POST requests, however i want to send back a zip file(or multiple files) in response to this request. Any help in this regard? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/277851-php-rest-api-how-to-send-a-file-as-a-response/ Share on other sites More sharing options...
gizmola Posted May 9, 2013 Share Posted May 9, 2013 Set the mime type correctly using header() and return the data. Here's a fairly typical example ... you'd have to fill in the details regarding how/where the original file is generated: <?php $path_to_zip = '/some/path/somefile.zip'; header("Content-Type: application/zip"); header("Content-Transfer-Encoding: Binary"); header("Content-Length:".filesize($path_to_zip)); header("Content-Disposition: attachment; filename=yourfile.zip"); readfile($path_to_zip); exit; As you can see most of this code is focused on the mechanics of the HTTP protocol. Quote Link to comment https://forums.phpfreaks.com/topic/277851-php-rest-api-how-to-send-a-file-as-a-response/#findComment-1429358 Share on other sites More sharing options...
cpordoegr Posted May 9, 2013 Author Share Posted May 9, 2013 Thanks for quick response. I find that the code you provided is working. However, the client, after receiving this zip file shows the contents of the file in browser (firefox e.g.), can we enfore it to be opened with a zip software (or ask for saving it)? Quote Link to comment https://forums.phpfreaks.com/topic/277851-php-rest-api-how-to-send-a-file-as-a-response/#findComment-1429376 Share on other sites More sharing options...
cpordoegr Posted May 9, 2013 Author Share Posted May 9, 2013 One more question about your suggested code. Will it be possible to identify the type of the file on client side (the one who will receive this zip file as a response)? Quote Link to comment https://forums.phpfreaks.com/topic/277851-php-rest-api-how-to-send-a-file-as-a-response/#findComment-1429381 Share on other sites More sharing options...
Jessica Posted May 10, 2013 Share Posted May 10, 2013 Did you include all of those headers? Both of those things are covered Quote Link to comment https://forums.phpfreaks.com/topic/277851-php-rest-api-how-to-send-a-file-as-a-response/#findComment-1429423 Share on other sites More sharing options...
cpordoegr Posted May 10, 2013 Author Share Posted May 10, 2013 Yes i included all headers. Actually now i can identify the type of the response content on receiving side. However i want to do the following Step 1. Create a new empty zip file Step 2. Write the received response body (a zip file with mutliple files in it) to this zip file. Step 1 is ok. But i am not sure how should i implement Step 2. I used "fwrite($zipfile, gzcompress($response_body));" but it is not working. Afterwards when i try to open this zip file with a zip software i get invalid zip file message. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/277851-php-rest-api-how-to-send-a-file-as-a-response/#findComment-1429437 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.