Remote file download with other name.

Discussion in 'PHP' started by John31, Feb 27, 2009.

  1. #1
    HI,

    What i'm trying to do, is to let the user download a file hosted elsewhere with a different name.
    for example, we have the file:
    http://www.xx.com/file.rar

    I want when the user downloads the file from my site( http://www.my.com/download?f=file.rar, using header("Location ..) to download the file with the name xxaddaa.rar instead of file.rar
    The simplest way would be to add headers and use curl to get and print the file, but i don't want that due to bandwidth loss.

    Please advise!

    Thanks
     
    John31, Feb 27, 2009 IP
  2. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #2
    Header used for the file name is Content-Disposition, but I doubt it works with 301 or 302 redirect. Here is example headers for outputting content as a file, try them:
    
    header('Content-Disposition: attachment; filename="filename.rar"');
    header('Content-type: application/force-download');
    header('Content-Transfer-Encoding: binary');
    PHP:
     
    wmtips, Feb 27, 2009 IP
  3. John31

    John31 Peon

    Messages:
    164
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Tried, didn't work. thanks.

    Any other ideeas?
     
    John31, Feb 28, 2009 IP
  4. American

    American Guest

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How about something like this?

    <?
    //set to the URL of the file you want to download:
    $inPath = "http://somepage.com/hello.jpg";
    //set to the local path where the file should be saved:
    $outPath = "/usr/local/htdocs/hello.jpg";
    
    $in = fopen($inPath, "rb");
    $out = fopen($outPath, "wb");
    
    while ($chunk = fread($in,8192) ) {
    fwrite($out, $chunk, 8192);
    }
    
    fclose($in);
    fclose($out);
    ?>
    PHP:
    You could even variablize that so you could have a URL submit form to submit the source and end product save. All you would have to do is wait for it to finish, then download directly from where you saved it.
     
    American, Feb 28, 2009 IP