I need to create a folder on root of my website server, called 'Docs'. In this folder I want to keep hello.doc file. Now I want this file to be downloaded even if user provides an incorrect file name (as far as the user's path fall under 'Docs' folder):
Which mean:
If someone downloads http://www.mysite.com/Docs/hello.doc He gets hello.doc downloaded.
However if someone downloads http://www.mysite.com/Docs/price.doc He gets the same file i.e. hello.doc renamed as price.doc when he downloads it.
This means I require something that renames a file depending upon the path it was given for the download. A good example of this you can see by going to http://www.newsoftwares.net/folderlock/
On this page if you click http://dl.filekicker.com/nc/file/130083 ... -lock6.exe
then a file named folder-lock6.exe will get downloaded.
If however you provide it with any other name, lets say your name is 'Mark' then you will get the same file downloaded (the same content of file) but renamed. Try it http://dl.filekicker.com/nc/file/130083-0M78/Mark.exe
I want to accomplish this for my site. Please help me understand and do it. A code snippet will be very appreciated.
Naming file on server depending upon download request
Moderator: General Moderators
Re: Naming file on server depending upon download request
Use mod_rewrite to redirect all requests in docs folder to single script
That script should send the headers to the browser, including the filename - the same as the requested filename
And for body of the response, send the contents of the hello.doc
Something like:
That script should send the headers to the browser, including the filename - the same as the requested filename
And for body of the response, send the contents of the hello.doc
Something like:
Code: Select all
header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename=' . $requestFileName);
header('Content-length: ' . filesize('hello.doc');
readfile('hello.doc');Re: Naming file on server depending upon download request
Thanks for your help. I am confused though, I don't know what language this is and how to code it. Can you kindly tell me step by step. Thanks!
Re: Naming file on server depending upon download request
This is PHP
However, just with configuring mod_rewrite to always load from the same file, probably the filename will remain the requested one. Give it a try:
However, just with configuring mod_rewrite to always load from the same file, probably the filename will remain the requested one. Give it a try:
Code: Select all
RewriteEngine On
RewriteRule .* hello.doc
Re: Naming file on server depending upon download request
Thanks so far.
Dear, can you please tell me whether the code that you just gave me for the .php (the one that you gave me earlier) do I have to just create a file of any name with a php format and keep that in the Docs folder along with the .htaccess.
I am very confused, kindly can you please show me the exact file names of the files to make, exact folders to create and exact code in each of these two files (php and .htacess). That would be awesome!
Please help me, tomorrow my boss is gonna get crazy if I don't get it done today.
Dear, can you please tell me whether the code that you just gave me for the .php (the one that you gave me earlier) do I have to just create a file of any name with a php format and keep that in the Docs folder along with the .htaccess.
I am very confused, kindly can you please show me the exact file names of the files to make, exact folders to create and exact code in each of these two files (php and .htacess). That would be awesome!
Please help me, tomorrow my boss is gonna get crazy if I don't get it done today.
Re: Naming file on server depending upon download request
Hi,
I've just tested it. You need only an .htaccess file in the docs folder:
If you need more control, with the same file you can redirect the request to a PHP script (replace hello.doc with the filename of the PHP script)
But just to keep the filename of the requested file, serving the predefined file, this is enough.
I've just tested it. You need only an .htaccess file in the docs folder:
Code: Select all
RewriteEngine On
RewriteRule (.*)\.doc hello.docBut just to keep the filename of the requested file, serving the predefined file, this is enough.
Re: Naming file on server depending upon download request
You're world's biggest genius and my super hero.
It worked mate, it worked. Yes! You made my day. Thank you Thank you Thank you.
It worked mate, it worked. Yes! You made my day. Thank you Thank you Thank you.