Downloading files and avoiding .txt extension

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
ReBrand
Forum Newbie
Posts: 10
Joined: Wed Jun 05, 2002 4:22 am

Downloading files and avoiding .txt extension

Post by ReBrand »

I'm wondering if this can be done with PHP. Here's the problem:

On the server are a number of files with eight character filenames and ??? extensions, where ??? is a number from 001 to 999. So the file could be thisfile.074, for example

Can I have a simple text box on screen where this information can be entered, and when the submit button is clicked, the file is written to the hard drive in a pre-dertermined location.

Or is that not possible? If not, any ideas?
ReBrand
Forum Newbie
Posts: 10
Joined: Wed Jun 05, 2002 4:22 am

Post by ReBrand »

The problem is, direct linking opens the file in the browser window and right-click, Save As removes the extension and offers only htm, html or txt extensions. Is there a way round this so the file is saved with the proper extension?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename='.$filename);
ReBrand
Forum Newbie
Posts: 10
Joined: Wed Jun 05, 2002 4:22 am

Post by ReBrand »

Thanks for your help but I'm not sure how to implement that. Right now there's a clickable link to the file so how do I make use of the above?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

somehow the browser must be under the impression that the document has the mime-type text/plain or text/html. Is it IE?
If there's no way to let your webserver perform the task maybe you can circumvent this behaviour by redirecting the link via a php-script.

Code: Select all

<a href="getFile.php?file=thisfile.074">thisfile.074</a>

Code: Select all

// some code that tests the userinput ("file" set AND valid?)
header('Content-type: application/octet-stream'); 
header('Content-disposition: attachment; filename='.$_GETїfilename]);
readfile($_GETїfilename]);
that's the very basics.
  • todo
  • validation, security issues
  • get rid of directories in filename='.$_GET[filename]
  • content-length header
ReBrand
Forum Newbie
Posts: 10
Joined: Wed Jun 05, 2002 4:22 am

Post by ReBrand »

Thanks again volka - I thought there may have been an easy solution but I see this isn't something for a newcomer to PHP so I'll need to spend some time working it out (I understand your stance on 'post-me-a-script' but I didn't realise it would be so involved).

Yes, it is IE & NetScape and ideally different file names with a fixed .txt extension should be used so the number that is now used as an extension isn't lost (which is vital) but that isn't an option so I'm just trying to find a quick way around someone else's bad system. I was hoping I could set the download path beforehand and when the link is clicked the file would be saved automatically, without the usual 'Save As' box appearing...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

think about it. You don't want any moron on this planet to be able to advice your browser to save files anywhere, do you?
Post Reply