Page 1 of 1

Downloading files and avoiding .txt extension

Posted: Wed Oct 30, 2002 10:39 am
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?

Posted: Wed Oct 30, 2002 3:36 pm
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?

Posted: Wed Oct 30, 2002 3:46 pm
by volka
try

Code: Select all

header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename='.$filename);

Posted: Wed Oct 30, 2002 4:13 pm
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?

Posted: Wed Oct 30, 2002 4:40 pm
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

Posted: Thu Oct 31, 2002 4:00 am
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...

Posted: Thu Oct 31, 2002 6:47 am
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?