我目前正在制作从服务器下载文件的控制器。这一切都发生在索引操作中:
public function indexAction() {
$schuurName = $this->_getParam('storageID');
$fileName = $this->_getParam('fileName');
$name = explode('.', $fileName)[0];
$path = '..' . DIRECTORY_SEPARATOR . 'schuren' . DIRECTORY_SEPARATOR . $schuurName . DIRECTORY_SEPARATOR . $fileName;
if (file_exists($path)) {
$mimeType = mime_content_type($fileName);
header('Content-Type: ' . $mimeType);
header('Content-Length: ' . filesize($path));
header('Content-Disposition: attachment; filename=' . $name . ';');
$resource = fopen($path, 'r');
while (!feof($resource)) {
$chunk = fread($resource, 4096);
echo $chunk;
}
$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
}
else {
echo 'file doesn''t exist';
}
}
所以下载工作现在,我正在测试它与一个725字节的图像。问题是……图像已损坏,因此无法看到/编辑。我在代码中做错了什么?
谢谢!
您应该使用二进制模式。使用"rb"标志。
来自php手册:如果你在处理二进制文件时没有指定'b'标志,你的数据可能会遇到奇怪的问题,包括损坏的图像文件和'r'n字符的奇怪问题。
http://www.php.net/manual/en/function.fopen.php