Yii Framework Forum

Why I can't download a pdf file correctly in yii1.1?


(George_lu) #1

0

Hi,guys! I am useing yii1.1, I want to implement a file upload and down load function, but after I upload my pdf file, I could’t download this file correctely. could you tell my why?

When I tried to download the file that I uploaded, I got an incomplete pdf file. In other words, I can’t open the pdf file that I downloaded.

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

    $upload=new Upload();

    $upload->downloadFile($path);
}

}

public function 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;}

}

I want to download my pdf file correctly.