Jump to content

Downloading files with Slim and cURL


NotionCommotion

Recommended Posts

I have the following REST API #1 endpoint which will download a non-text (PCAP) file to a web browser.

 

API #1 doesn't directly have the file, and must make another HTTP request to API #2 using cURL.

 

For small files, it works fine, but for larger files, it provides a file with zero size.

 

Any ideas where the problem is?

 

Thank you

 

API #1

$app->get('/{guids}/{guid}/logs/{id:[0-9]+}', function (Request $request, Response $response, $args) {
    $config=$this->get('settings')['server'];
    $query=$request->getUri()->getQuery();
    $query=$query?"?$query":null;
    $url=$config['ip']._VER_."/guids/$args[guid]/logs/$args[id]$query";
    $context = stream_context_create(['http'=>['header'=>'X-Access-Key: '.$config['key']]]);
    $fh = fopen($url, 'rb', false, $context);  //r or rb?
    $stream = new \Slim\Http\Stream($fh);
    $headers = $stream->getMetadata()['wrapper_data'];
    $forwardHeader=[
        'Content-Description'=>'File Transfer',
        'Content-Type'=>'application/octet-stream',
        'Content-Transfer-Encoding'=>'binary',
        'Content-Disposition'=>'attachment; filename="replaced_name"',
        'Expires'=>0,
        'Cache-Control'=>'must-revalidate, post-check=0, pre-check=0',
        'Pragma'=>'public',
        'Content-Length'=>false,   //Possible to get stream length?
    ];
    foreach ($headers as $header) {
        $header=explode(':',$header);
        if($header && count($header)==2 && isset($forwardHeader[$header[0]])){
            //Should I really be modifying the $response?
            $response=$response->withHeader($header[0],trim($header[1]));
            unset($forwardHeader[$header[0]]);
        }
    }
    //Is this necessary?
    foreach ($forwardHeader as $key=>$value) {
        if($value!==false){
            $response=$response->withHeader($key,$value);
        }
    }
    return $response->withBody($stream);
});

API #2

$app->get(_VER_.'/guids/{guid}/logs/{id:[0-9]+}', function (Request $request, Response $response, $args) {
    $doc=$this->get('Guids')->getUploadInfo($args['guid'],$args['id']);   //Returns object with path, name, size, and will throw an exception if path doesn't exist
    $fh = fopen($doc->path, 'rb'); //r for readonly, and b for binary?
    $stream = new \Slim\Http\Stream($fh);
    return $response->withBody($stream)
    //->setOutputBuffering(false)
    ->withHeader('Content-Description', 'File Transfer')
    ->withHeader('Content-Transfer-Encoding', 'binary')
    ->withHeader('Content-Type', 'application/octet-stream')
    //->withHeader('Content-Type', 'application/force-download')    //Don't use?
    //->withHeader('Content-Type', 'application/download')          //Don't use?
    ->withHeader('Content-Disposition', "attachment; filename='$doc->name'")
    ->withHeader('Expires', '0')
    ->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
    ->withHeader('Pragma', 'public')
    ->withHeader('Content-Length', $doc->size);
});
Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.