我从 PHP.net 读取文件页面中获取了这段代码:
<?php
// Action controller
public function someAction() {
$response = $this->_response;
// Disable view and layout rendering
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout()->disableLayout();
// Process the file
$file = 'whatever.zip';
$bits = @file_get_contents($file);
if(strlen($bits) == 0) {
$response->setBody('Sorry, we could not find requested download file.');
}
else {
$response->setHeader('Content-type', 'application/octet-stream', true);
$response->setBody($bits);
}
}
?>
它适用于大多数文件,但是当我在文件名中有空格的文件上测试它时,它说它找不到请求的文件。有什么建议,或者有没有更好的方法在 Zend Framework 中执行 readfile,其中文件名可以包含空格?