为什么我无法在yii1.1中下载文件

时间:2018-12-21 06:08:29

标签: php yii

伙计们!我正在使用yii1.1,我想实现文件上传和下载功能,但是在我上传pdf文件后,无法正确下载此文件。你能告诉我为什么吗?

当我尝试下载上传的文件时,我得到的PDF文件不完整。换句话说,我无法打开下载的pdf文件。

public function actionDownload(){
    $path = Yii::getPathOfAlias('/yiiroot/trackstar/protected/uploads/')."mypdffile.pdf";

    $upload=new Upload();

    $upload->downloadFile($path);
}

}

公共函数downloadFile($ fullpath){

    if(!empty($fullpath)){


        header("Content-type:application/pdf"); //for pdf file
        //header('Content-Type:text/plain; charset=ISO-8859-15');
        //if you want to read text file using text/plain header

        header('Content-Disposition: attachment; filename="'.basename($fullpath).'"');

        header('Content-Length: ' . filesize($fullpath));

        readfile($fullpath);
        Yii::app()->end();
    }
    else {return false;}

}

我想正确下载pdf文件。

1 个答案:

答案 0 :(得分:0)

它通过以下更改起作用。

public function downloadFile($fullpath){
        $dir = Yii::getPathOfAlias('application.uploads');
        $filename= $fullpath;

        if(!empty($fullpath)){
            $file = $dir."\\"."$filename";


            header("Content-type: application/pdf");
            header("Content-Disposition: inline; filename=$filename");
            @readfile($file);


            Yii::app()->end();
        }
        else {return false;}

    }