Zend Framework 操作中文件名中的空格类似于 readfile
我从 PHP.net readfile 页面获取了这段代码:
<?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 中执行读取文件,其中文件名可以包含空格?
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 {
$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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自
file_get_contents()
手册页:(编辑:使用
rawurlencode()
将空格转换为%20
而不是+
)因此,您需要
rawurlencode()
使用前的文件名它:From the
file_get_contents()
manual page:(Edit: Use
rawurlencode()
to convert spaces to%20
instead of+
)So, you need to
rawurlencode()
the filename before using it:我用字符串替换去掉了文件名中的空格,现在它可以工作了:
I got rid of the space in the filename with a string replace and now it works: