开发者

Spaces in filename in Zend Framework action similar to readfile

I grabbed this bit of code from the PHP.net readfile page:

<?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 {
 开发者_Python百科       $response->setHeader('Content-type', 'application/octet-stream', true);
        $response->setBody($bits);
    }
}

?>

It works great with most files but when I test it on a file with spaces in the filename, it says it can't find the requested file. Any suggestions, or is there a better way to do a readfile in Zend Framework where the filename can have spaces in it?


From the file_get_contents() manual page:

Note: If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

(Edit: Use rawurlencode() to convert spaces to %20 instead of +)

So, you need to rawurlencode() the filename before using it:

$file = rawurlencode('whatever.zip');


I got rid of the space in the filename with a string replace and now it works:

$file = str_replace(" ","%20",$file);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜