2

我从 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,其中文件名可以包含空格?

4

2 回答 2

1

file_get_contents()手册页:

注意:如果您使用特殊字符(例如空格)打开 URI,则需要使用 urlencode().

(编辑:rawurlencode()用于将空格转换为,%20而不是+

所以,你需要rawurlencode()在使用它之前的文件名:

$file = rawurlencode('whatever.zip');
于 2009-12-09T18:45:20.577 回答
0

我用字符串替换去掉了文件名中的空格,现在它可以工作了:

$file = str_replace(" ","%20",$file);
于 2009-12-10T01:07:03.087 回答