php – 使用Zend Framework 2正确处理二进制数据和部分内容的方法

我想允许提供具有某种访问控制的二进制文件.由于控件相当复杂,我不能简单地让Apache提供文件,我必须使用我的Zend Framework 2应用程序通过
PHP提供服务.行动如下:

public function sendAction() {
        $filename = /* database action */;
        $size = filesize($filename);
        $response = $this->getResponse();

        if($this->getRequest()->getHeaders()->has('Range')) {
            list($unit, $range) = explode('=', $this->getRequest()->getHeaders()->get('Range')->toString());
            $ranges = explode(',', $range);
            $ranges = explode('-', $ranges[0]);

            $start = (int)$ranges[0];
            $end = (int)(isset($ranges[1]) ? $ranges[1] : $size - 1);
            $length = $start - $end;

            $response->getHeaders()->addHeaders(array('Content-Type' => 'audio/mpeg', 'Accept-Ranges' => 'bytes', 'Content-Length' => $length - 1));
            $response->setStatusCode(206);

            $f = fopen($filename, 'r');
            if($start) fseek($f, $start);
            $out = '';
            while($length) {
                $read = ($length > 8192) ? 8192 : $length;
                $length -= $read;
                $out .= fread($fp,$read);
            }
            fclose($f);

            $response->setContent($out);
        } else {
            $response
                 ->setContent(file_get_contents($filename))
                 ->getHeaders()->addHeaders(array('Content-Type' => 'audio/mpeg', 'Accept-Ranges' => 'bytes'));
        }

        return $this->getResponse();
}

好吧,我确信这是非常低效的,因为文件总是在服务之前完全加载到RAM中.

但是,这似乎不起作用.当我尝试访问该文件时,我在Chrome中获得了正确的audio / mpeg播放器,但随后浏览器取消了请求并停止了.我根本无法播放音频.

我无法在网上找到关于如何在Zend 2中以正确的方式实现206响应的任何提示,也许有人可以在这里帮助我.

最佳答案 你应该使用流.

我的应用程序的示例代码

public function documentAction()
{
    $name = $this->params('name');
    try {

        if (!$this->getServiceLocator()->get('AuthStorage')->hasIdentity()) {
            throw new \Exception('You must login.');
        }

        $file = getcwd().'/data/uploads/'.pathinfo($name)['basename'];
        if (file_exists($file)) {
            $response = new \Zend\Http\Response\Stream();
            $headers = new \Zend\Http\Headers();
            $headers->addHeaderLine('Content-type', 'application/pdf');
            $response->setHeaders($headers);
            $response->setStream(fopen($file, 'r'));
            return $response;
        } else {
            throw new \Exception('File not exist');
        }
    }
    catch (\Exception $e) {
        $this->flashMessenger()->setNamespace('error')->addMessage('404');
        return $this->redirect()->toUrl('/');
    }
}    
点赞