Hi,

I'm trying to create a basic download manager. I want to be able to upload files and then as you upload them they are added to a mysql database aswell, that includes the filename and the extension (that part im fine with.)

Then I want to allow people to download the files, without revealing the filename (to prevent leeching) I.e each file is downloaded with a set filename, although with the relevant extension.

I have this code which I think works for .midi files:

$filepath = 'http://mysiteaddress.com/' . $row['file_name'] . '.' . $row['extension']; 
header('Content-Disposition: attachment; filename="' . $filepath . '"'); 
header('Content-Type: x-music/x-midi'); 
readfile($filepath); 
header('Connection: close');
        exit();

Then..
for au files: header('Content-Type: audio/basic');
for mp3's: header('Content-Type: audio/mp3');
for wavs: header('Content-Type: audio/x-wav');

My question is this. Is this the best way about doing what I plan to do? and further what headers should I use for .zip files, jpegs and gifs.

Also, if I included the size of the file in the database how could i include this in the download code I wrote above.

ANY help will be most appreciated.

Thanks.

    First of all, the 'filename' paramter in the Content-Disposition should be a filename, not a file path. So if the address is http://mysiteaddress.com/files/theFile.zip, your header should look like "filename=theFile.zip". In addition, I've read about some compatibility issues when the filename is not surrounded in quotes (especially if there is a space in the filename).

    Second, if you are able to use it, look at the man page for [man]mime_content_type/man so that you can automatically detect the mime type of the file.

    Third, you can include the filesize by adding a Content-Length parameter and using [man]filesize/man or a size returned from a DB result.

    Fourth, why are you using an http:// protocol in the filepath? Is the file not on the same server as this script? If not, why can't you just redirect them to a script on the remote server? If so, do NOT use the http protocol!

      5 days later
      Write a Reply...