phpcsvhttp-headerscontent-disposition

What content-disposition to use when giving response to a cURL request?


Let's say we have a client (which is actually a server side PHP script), and a server (which is a server side PHP script too).

The client makes a HTTP request to the server via cURL. The client accepts text/csv, so the corresponding header is set and the client would like to save the response into a file, so the CURLOPT_FILE option is properly set.

The question is whether the server when serving the request and sends back the CSV "encoded" content, should it use the inline or the attachment as the value for Content-Disposition header?

A very simple pseudo-code for testing: the server does something like this:

if (file_exists($attachment_location)) {
    header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
    header("Cache-Control: public"); // needed for internet explorer
    header("Content-Type: text/csv");
    header("Content-Length:".filesize($attachment_location));
    header("Content-Disposition: inline");
    readfile($attachment_location);
    die();
} else {
    header($_SERVER["SERVER_PROTOCOL"] . " 404 Not found");
}

Or should it be something like:

<?php
$attachment_location = "./c3m.csv";

if (file_exists($attachment_location)) {
    header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
    header("Cache-Control: public"); // needed for internet explorer
    header("Content-Type: text/csv");
    header("Content-Transfer-Encoding: Binary");
    header("Content-Length:".filesize($attachment_location));
    header("Content-Disposition: attachment; filename=c3m.csv");
    readfile($attachment_location);
    die();
} else {
    header($_SERVER["SERVER_PROTOCOL"] . " 404 Not found");
}

Solution

  • cURL doesn't care about the Content-Disposition, so it should have no bearing on what value you give it.