Download protected content


I’m setting up a list of product on my website using yiiframework.

These products are managed with subdirectories containing images and downloadable pdf files.

I can put images on webproject/images/ to simplify my work but I would like the downloadable files would be put in proteced/site/pages/products with the chance to make them downlaodable after the login or accessing them just through the website (I still must decide it).

How can I access these files?


You can use Request.sendFile

I read this: so I tried to understand how to use xSendFile to make a pdf file downlaodable.

For example:

  • the page that will call xSendFile is placed in /protected/views/site/myfile.php

  • the PDF file is placed in /protected/views/site/files/myPDFfile.pdf

My first attempt, in myfile.php was:

<a href="'.Yii::app()->request->xSendFile("/files/myPDFfile.pdf",array(


"terminate"=>false)).'">Click here to download myfile</a>

But it doesn’t work.

How can I make it downloadable through a clickable link?

You need to give the proper path to xSendFile, relative path do not work for this method… but note that what you wrote above is not even a relative path…

xSendFile needs a special setup on the server… check this wiki for more info -


the problem is that I won’t have my own server to run this website (it’s a website based on Yii, not a real web-application) so it will be stored in a simple web hosting service.

That take me to the start question.

I think I must have posted this in the past, though I can’t recall where or when.

So I posted it again on github:

I perfected this script over a period of several years - it deals with a number of problems that are hard to get around, and has been used on numerous sites.

It supports download managers and multi-threaded downloads and HTTP resume; it stream out the data in segments rather than using a chunk of memory as large as the file you’re sending; it reports (under Apache) whether the file was fully downloaded and received by the user, so you can track/count downloads for security purposes.

All of those features are missing in CHttpRequest::sendFile() which in my opinion is much too basic. As I recall, I’ve encouraged the framework team to adopt my implementation in the past, but they weren’t interested - I don’t recall why…


you should encourage them again…this is a nice implementation

Thanks :slight_smile:

It’s easy enough to extend [color="#1C2837"][size=“2”]CHttpRequest.[/size][/color]

[color="#1C2837"][size=“2”]Especially starting with Yii 1.9. :)[/size][/color]

[color="#1C2837"] [/color]

[color="#1C2837"][size="2"]Please, wiki it.[/size][/color]

mindplay, that is beautiful code