Download File endpoint returns binary data , but outputting with PHP still results in corrupted file
Could anyone help me diagnose an issue I am experiencing when downloading files from the Box content endpoint using PHP?
I'm able to upload content fine and I receive the correct response of RAW data when using the download endpoint. The trouble starts there however..
In the bolded portion when I attempt to output the raw data to the user for download, I end up with a corrupted file when trying to open the file. Does anyone have some tips for forcing the download of this raw data correctly to the web browser? I'm using PHP in this example, but if there is a better approach, I am open to investigating.
I am pulling the file name and other info from the "Get File's Info" endpoint, but have omitted it to remove extra stuff from my code example.
$url = "https://api.box.com/2.0/files/** file id **/content?access_token=".$token; $header = ''; $header = array(); ////////////////////////////////////////// // Set Required CURL options ////////////////////////////////////////// $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // don't return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle compressed CURLOPT_USERAGENT => "test", // who am i CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect CURLOPT_TIMEOUT => 120, // timeout on response CURLOPT_MAXREDIRS => 10); ////////////////////////////////////////// // Specify API endpoint ////////////////////////////////////////// $ch = curl_init ($endpoint); ////////////////////////////////////////// // Write options ////////////////////////////////////////// curl_setopt_array ( $ch, $options ); ////////////////////////////////////////// // Execute ////////////////////////////////////////// $header['response'] = curl_exec ( $ch ); $header['errno'] = curl_errno ( $ch ); $header['errmsg'] = curl_error ( $ch ); $header['httpcode'] = curl_getinfo ( $ch, CURLINFO_HTTP_CODE ); ////////////////////////////////////////// // Close connection /unbind ////////////////////////////////////////// curl_close ($ch); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=** some file name **'); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); print($header['response']);
-
Hello ,
I understand that you are able to download a file but it appears with no extension. Is that the situation? Have you tried to set the extension manually and see if it opens or the document is really corrupted?
I would remove, maybe, the line
header('Content-Type: application/octet-stream');
I am no expert on PHP but I would say that you are then forcing the element to be printed to be treated as a generic element and not the particular one you expect.
Regards
-
Thanks for the reply. Depending on the file type, I have tried a couple of different Content-Type headers like "image/png" etc.
I ended up settling with a solution that actually works okay and uses the Box expiring link embed element. Attaching what I did for example purposes.
$url = "https://api.box.com/2.0/files/".$file_id."?fields=expiring_embed_link&showDownload=true&access_token=".$token; $header = ''; $header = array(); ////////////////////////////////////////// // Set Required CURL options ////////////////////////////////////////// $options = array( CURLOPT_RETURNTRANSFER => true, // return web page CURLOPT_HEADER => false, // don't return headers CURLOPT_FOLLOWLOCATION => true, // follow redirects CURLOPT_ENCODING => "", // handle compressed CURLOPT_USERAGENT => "test", // who am i CURLOPT_AUTOREFERER => true, // set referer on redirect CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect CURLOPT_TIMEOUT => 120, // timeout on response CURLOPT_MAXREDIRS => 10); ////////////////////////////////////////// // Specify API endpoint ////////////////////////////////////////// $ch = curl_init ($endpoint); ////////////////////////////////////////// // Write options ////////////////////////////////////////// curl_setopt_array ( $ch, $options ); ////////////////////////////////////////// // Execute ////////////////////////////////////////// $header['response'] = curl_exec ( $ch ); $header['errno'] = curl_errno ( $ch ); $header['errmsg'] = curl_error ( $ch ); $header['httpcode'] = curl_getinfo ( $ch, CURLINFO_HTTP_CODE ); ////////////////////////////////////////// // Close connection /unbind ////////////////////////////////////////// curl_close ($ch); if($header['httpcode'] == 200) { $file = json_decode($header['response'], true); header('location: '.$file['expiring_embed_link']['url'].'?showDownload=true'); }
-
Hello ,
when you use the REST API to download the document it returns a URL like this one: "https://dl.boxcloud.com/d/1/uydh28ktI4yTOgpeh7jf5gSVbqxxGVREqEWQgK7ELLubq9Hk7WSUm3M5LJ0fl8CGrtw2IH8PU3YfPU(...)nmo11K3ALKjev7Eg2R86cuq3EjZI1xtC3ZMUOvxpzidOMGXVwh8OlMNb3jfI3D138Nrb3wLgjtlyu9fOIeYVhGhf2nL460paarA../download"
So... if you replace this piece
'.$file['expiring_embed_link']['url'].'?showDownload=true'
by the URL
header('location: https://dl.boxcloud.com/d/1/uydh28ktI4yTOgpeh7jf5gSVbqxxGVREqEWQgK7ELLubq9Hk7WSUm3M5LJ0fl8CGrtw2IH8PU3YfPU(...)nmo11K3ALKjev7Eg2R86cuq3EjZI1xtC3ZMUOvxpzidOMGXVwh8OlMNb3jfI3D138Nrb3wLgjtlyu9fOIeYVhGhf2nL460paarA../download');
does it not work?
Completely guessing here to be sincere 🙂
Regards
Please sign in to leave a comment.
Comments
3 comments