简体   繁体   中英

Yii2 - download corrupted file

I am working on existing products and i am trying to resolve problem with downloading xlsx file. When i download file through file manager, file is allright. But when i download file through Yii app, the file is corrupted.

Here is my code

public function actionCallLog($hash, $filename)
    {
        var_dump(
            $hash, $filename
        );

        ignore_user_abort(true);
        set_time_limit(0);
        

        $path = "/var/www/html/uploads/call_log/".$hash.'/';

        $dl_file= basename($filename).'.xlsx';
        if (!file_exists($path.$dl_file)) {
            $dl_file= basename($filename).'.csv';
        }
        $fullPath = $path.$dl_file;

        if ($fd = fopen ($fullPath, "r"))
        {

            $fsize = filesize($fullPath);
            $path_parts = pathinfo($fullPath);
            $ext = strtolower($path_parts["extension"]);
            switch ($ext)
            {
                case "xlsx":
                header('Content-Description: File Transfer');
                header("Content-type: application/xlsx");
                header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
                break;
                case "csv":
                header('Content-Description: File Transfer');
                header("Content-type: application/csv");
                header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\"");
                break;
                // add more headers for other content types here
                default;
                header('Content-Description: File Transfer');
                header("Content-type: application/octet-stream");
                header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
                break;
            }
            header("Content-length: $fsize");
            header("Cache-control: private");
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Expires: 0');
            header('Pragma: public');
            while(!feof($fd))
            {
                $buffer = fread($fd, 2048);
                echo $buffer;
            }
        }
        fclose ($fd);
        exit;
    }

Does anybody know where can be the problem?
Thanks

try add the response format

\Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;

and for xlsx the content type should be

'application/vnd.ms-excel'

Try turning off all the header instructions (and also the "echo $buffer;" as that might be a lot on screen) and see if any warnings or errors is printed on screen when opening the url for the file download.

If the corrupted file has a size, your code might actually give a warning or generate other output which will become part of the file because it is also echo'ed. Obviously that extra plain text output by your code would turn your correct xlsx into a corrupt file. You might be able to get that text by opening your corrupted file in a text-editor as it would show in there in plain text (probably at the start).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM