This extension is a simple implementation to cloak your original location of files to be downloaded. That way, no direct links to your file will be available to visitors.
Requirements ¶
- Yii 1.1.6 or above
- mod_xsendfile (optional) to allow download of large files with ease
Usage ¶
How to use:
- Download and extract EFileCloaker in your extension directory.
- Modify your configuration file to auto import the extension folder example:
<?php
'import'=>array(
'application.extensions.*',
),
?>
- Call the widget with parameters: (1) OriginalFileName as displayed name; (2) OriginalUrl as direct link to file. Example:
<?php
$this->widget('application.extensions.EFileCloaker', array(
'OriginalFileName'=>'Demo File',
'OriginalUrl'=>'../../file/demofile.pdf',
));
?>
- Create an action to download files. Example:
<?php
public function actionDownload()
{
$uncloak = new EFileCloaker();
// To get original Url for further processing
// $originalUrl = $uncloak->getOriginalUrl();
// Downloads the file indicated by the $_GET key
$uncloak->download();
// Or downloads the file while specifiying
// some actions before actual downloads happens
// with a callback function
// $uncloak->download(function($key, $originalUrl) {
// die($key . $originalUrl);
// });
}
?>
- Done
Good idea but
thanks for sharing, but could you please provide a more detailed usage sample? for me it's not that clear the action and what i am supposed to pass as parameters
thanks very much!
Only one word...
readfile()... It can be nice for, i don't know, for small pictures maybe, but it will kill you server when you use it with big files. Read about this function in PHP manual and use something better, for example x-sendfile.
Missing doc
Missing documentation. I don't see how it works.
Thanks for the comments
Sorry guys, I havn't got the chance to update it yet or add more documentation..
I'll try to submit a working example this week.
Thanks to Pomstiborius for the advice. I'll add an option for that.
As I understand x-sendfile is an apache extension while I originally developed it in IIS.
x-sendfile
Since 1.1.6 there is CHttpRequest->xSendFile() and you can read more about this here. You can use it on Apache, Lighttpd v1.5, Cherokee, Lighttpd v1.4, Nginx and Cherokee.
About readfile - it is better (but only a little ;) ) to read file in small parts using fread() instead of loading whole file at once. But still you use a lot of memory and with big files it can be a problem. I've tried readfile() with up to 20 MB pdf files and it was quite ok, but wouldn't recommend for anything bigger.
x-sendfile
Yes, I have noticed that CHttpRequest have both a xsendfile and a sendfile method that I could use since you pointed it out at your first comment Pomstiborius. I hadn't had the chance to do a thorough debug, but at the first attempt, I didnt succeed using xsendfile with IIS.
I do plan to update the method this week. Hopefully I could publish it again soon.
Again, thanks for pointing it out.
Error
Sorry but it gives me:
Undefined index: id
/var/www/html/findyournotes/protected/extensions/EFileCloaker.php(103)
091 }
092 $this->_setOriginalFile();
093 $this->_cloakUrl();
094 $this->_createMarkUp();
095 }
096
097 public function download()
098 {
099 $cc = new Controller($this->_genRandomNumber());
100 $ses = new CHttpSession();
101 $ses->open();
102
103 $key = $_GET[$this->_cloakPrefix]; ///this line
Error...
What to do?
Thanks for your ext.!!
More details, please
Could you be more specific jzhong5?
When did this error occured?
Have you tried the examples? Did it work?
Not works
Yes, also not works. And I think the code in your demo is exactly the same as the code in my view+controller except the file path...so...
anyway
it gives me
Undefined index: id
/var/www/html/xxxxxx/protected/extensions/EFileCloaker.php(103):error line
the line 103 is: $key = $_GET[$this->_cloakPrefix]; ///this line
I guess the problem is here:
private $_cloakPrefix = "id";
it occurs when you click on the download link.
and it redirects me to /download/f12f3fahf blahblah
and gives me this error
any ideas?
.thanks
PHP Configuration
Would you mind sending me your php configuration? The configuration from phpinfo() would be fine. If you do mind sending it here, you could send it to my mail.
I do have an idea, and I'd try to modify it, but I would also like to test it with your php configuration. Therefore, would you mind sending me your php configuration? The configuration from phpinfo() would be fine. If you do mind sending it here, you could send it to my mail.
Thanks.
sure
Have already sent to you at bS.xx7_37@yahoo.com
Best,
jiaming
Path URL issue.
As I checked further in, it seems the issue there is the path url type which wasn't anticipated in my previous version of the extension. As for now, I did a little update to fix the issue.
I hope this fix the issue you are facing, jzhong5.
Thanks
thank you for your quick fix.
I will try it tonight when I get home.
Also, is there any chance that I can use your ext and do NOT just display a filename?
Instead, a button? or I can use buttons in bootstrap?
Shall I use beginWiget .. or something like that?
Anyway I will give you a report about this version this night.
Thanks
Sorry...
Sorry but still not work.
This time it keeps giving me file not found.
I can sure it's ok with my path. I tested my path with the normal download link.
Path
Are you sure it is the right path?
To be sure, the path is supposed to be a relative path towards the root directory.
Unfortunately, no. I haven't made an option to change how it looks. I'll note that as a future feature. As for now, you may edit how it looks like by editing directly the code under the function _createMarkup.
A bug about xsendfile
Is there any chance i can use RewriteEngine to get rid of index.php
and use xsendfile at the same time...?
Each time, I use my rewriteEngine, xsendfile gives me index.php not found.
So wired...
If this can not be fixed...We can't use xsendfile....
Any ideas?
PS: I just bootstrapped your ext. I will send you the codes later.
Back again
Hi, sorry to be back after a while. Lifes been crazy lately.
I haven't actually tried to use the mod_rewrite with the extension.
Thanks for the info, I'll see what I can do about it in a while.
Hope to reach you back soon.
PS: Thanks if you did. Would like to see the updated code :)
thanks for your reply
Looking forward to hearing from you!
The problem is because my rewrite rule will erase all index.php in the url...
Anyway....I will post the bootstrap codes later. it's pretty simple.
Thanks for your amazing ext.
Cheers
x-sendfile fix
Hi jzhong5,
I couldn't really find out what cause the unexpected behavior, but I did implement a tweak to make it work. I've tested it on my system and it seems that xsendfile works quite well with mod_rewrite on my system.
Do give it a try.
Cheers.
Seems okay
Actually I'm using similar rules like yours.
I tried it here -> http://expressthisout.com/demo_efc/site/index/ and it seems fine.
This is the rule i'm using:
RewriteEngine on # if a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward it to index.php RewriteRule . /demo_efc/index.php
And at the root, I used another one with this rule:
# Remove www RewriteCond %{HTTP_HOST} ^www\.(.*) [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
Thanks for your reply
RewriteRule . /demo_efc/index.php
Would you please explain a little bit about what this line for?
Your website works fine because when I download the file, the index.php will appear in your url....but mine is not.
Do I need to add some similar lines like this?
Confirm the index.php on your code
Yes, it's supposed to have index.php on the download part.
I specifically made it to be like that for the extension. Look at the code to see what I mean:
Yii::app()->urlManager->setUrlFormat("get"); Yii::app()->urlManager->showScriptName = true;
I specified there so that it would generate the url by the get format, not the path format.
As for the meaning of:
~~~
RewriteRule . /demo_efc/index.php
~~~
It just simply changes the current path (eg. http://expressthisout.com/demo_efc/site/index/) to include the index.php (eg. http://expressthisout.com/demo_efc/index.php/site/index/)
If i just use:
~~~
RewriteRule . index.php
~~~
The path would be translated from http://expressthisout.com/demo_efc/site/index/ to http://expressthisout.com/index.php/site/index/ which would most likely return an error of 404.
Thanks so much
In your website...
your download link is:
http://expressthisout.com/demo_efc/index.php/site/download/cc5d906c
So you put your index.php right after demo_efc
however, in mine...
the download link is
http://www.123.com/myModel/download/cc4hf2f2
Where should I put the index.php....? Why is the download path so different....?
Sorry about so many problems...
Thanks so much!
Best,
jiaming
Another fix
Hi jzhong5,
after i and some of my friends tested it out, the reason of the xsendfile might not be the index.php. After some testing and debugging, the more likely cause is the path generated is to relative and since url_rewrite do some manipulation in the url path, the relative path seems to get affected.
I've changed the way for it to take the path so that it would take it directly not by URL, but from the file system.
Would you mind testing the new version of it and see how it works with you?
Thanks so much .
Works like a charm.
Thanks so much for your extension and your help!!!!!!!
Would you mind explaining a little bit about " but from the file system."
So you are getting the path from the database directly?
Anyway, it's such a great extension! and xsendfile works properly now!
Great
Thats great :)
What I meant by directly from the file system is by accessing the local path.
For web files, they usually have several paths that we could use to access them. The one I meant by URL would be a path like -> http://www.example.com/some/path/to/file while a filesystem path i meant would be something like -> /usr/global/www/some/path/to/file
I hope that clears your confusion.
That's great. thank you.
Thanks. that is great!
:-D
About get the id of the view page
Hi, how are you doing these days.
My application is gonna launch next month and I meet a new problem about downloading.
For example. I do have a download button in the view page...
http://www.123.com/topic/22
and the download link would be
http://www.123.com/topic/download/sfa21fsf
Is there a way I can get the id(in this case 22) of the previous view page?
since the path of the download page does not contain id so I can't just use loadModel in the actionDownload of TopicController...
I want to collect how many times the file has been downloaded...so I need to get the id of that topic....
Thanks so much!!!
http referer
I'm doing fine, thanks :)
That's good news to know your application is going to be launched soon, congratulations.
If I were to rephrase your question, would it be the same as: "How to get the page calling the download file?"
If it is so, you could try using $_SERVER['HTTP_REFERER'], do some google to know more about it. Since it's already in a global variable, I didn't include it in the extension.
I did, however, push another update of the extension allowing to use closures to execute a function exactly before it downloads the file. Check the extension page above for details.
It may not be reliable
Hi, I know $_SERVER['HTTP_REFERER'], but it may not be reliable....As some browsers just send blank string for it or even not send at all....So it's not a good idea to get the previous page's url...
It is wired each time i use loadModel in the actionDownload it gives me 404 error...
getCallingUrl
Hi jzhong5,
I agree, sometimes it's not reliable. Try to use the latest version of the component. I add a method getCallingUrl() which returns the page where the widget is placed.
Try it, I hope it satisfies your need.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.