Save remote file that pushes headers to force download

前端 未结 2 1806
孤独总比滥情好
孤独总比滥情好 2021-01-17 00:37

I can download remote files using PHP but how do you download from a link that pushes headers out? I mean, you can click on some links and it will force a download and prese

2条回答
  •  旧时难觅i
    2021-01-17 00:48

    Just tried to reproduce situation. Gubmo is right, this download method works for me with Content-Type: application/octet-stream and Content-type: application/force-download headers.

    As explained here, HTTP 410 means that URL requested by the client is no longer available from that system. This is not a 'never heard of it' response, but a 'does not live here any more' response. Maybe they have some kind of antileach system.

    This should be investigated. If they need cookies -- stream-context-create can help. Or maybe they check referer. But I am almost sure that problem is not in headers.

    Hope this helps.

    UPD Sample code you've asked about.

    // file to download -- application/octet-stream
    $remoteFile = 'http://dev/test/remote/send.php';
    // file to download -- application/force-download
    $remoteFile = 'http://chtyvo.org.ua/authors/Skriabin_Kuzma/Ya_Pobieda_i_Berlin.rtf.zip';
    // file to store
    $localFile = 'kuzma.zip';
    
    $fin = fopen($remoteFile, "r");
    if (!$fin) {
        die("Unable to open remote file");
    }
    
    $fout = fopen($localFile, "w");
    if (!$fout) {
        die("Unable to open local file");
    }
    
    while (!feof($fin)) {
        $line = fgets($fin, 1024);
        fwrite($fout, $line, 1024);
    }
    
    fclose($fout);
    fclose($fin);
    

    Same as yours.

提交回复
热议问题