Naming file on server depending upon download request

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
maxbrayne
Forum Newbie
Posts: 4
Joined: Sat Sep 05, 2009 5:56 am

Naming file on server depending upon download request

Post by maxbrayne »

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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Naming file on server depending upon download request

Post by Darhazer »

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:

Code: Select all

header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename=' . $requestFileName);
header('Content-length: ' . filesize('hello.doc');
readfile('hello.doc');
maxbrayne
Forum Newbie
Posts: 4
Joined: Sat Sep 05, 2009 5:56 am

Re: Naming file on server depending upon download request

Post by maxbrayne »

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!
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Naming file on server depending upon download request

Post by Darhazer »

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:

Code: Select all

RewriteEngine On
RewriteRule .* hello.doc
 
maxbrayne
Forum Newbie
Posts: 4
Joined: Sat Sep 05, 2009 5:56 am

Re: Naming file on server depending upon download request

Post by maxbrayne »

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.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Naming file on server depending upon download request

Post by Darhazer »

Hi,

I've just tested it. You need only an .htaccess file in the docs folder:

Code: Select all

RewriteEngine On
RewriteRule (.*)\.doc hello.doc
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.
maxbrayne
Forum Newbie
Posts: 4
Joined: Sat Sep 05, 2009 5:56 am

Re: Naming file on server depending upon download request

Post by maxbrayne »

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.
Post Reply