簡體   English   中英

下載文件后,它已損壞

[英]After downloading file, it is corrupted

我目前正在制作一個控制器,以從服務器下載文件。 這一切都發生在索引動作中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM