PHP:下载没有直接链接的pdf文件

时间:2017-06-22 08:13:17

标签: downloading-website-files

我正在尝试从此网址下载pdf文件:

http://knihy.cpress.cz/?p=actions&action=download/file&value=files&id=149253

我尝试通过file_get_contents获取文件,但它只下载了没有实际pdf的php文件。

有没有办法下载这个文件?

非常感谢!

1 个答案:

答案 0 :(得分:1)

<?php

    function collect_file($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, false);
        curl_setopt($ch, CURLOPT_REFERER, "http://www.xcontest.org");
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $result = curl_exec($ch);
        curl_close($ch);
        return($result);
    }

    function write_to_file($text,$new_filename){
        $fp = fopen($new_filename, 'w');
        fwrite($fp, $text);
        fclose($fp);
    }


    // start loop here

    $new_file_name = "testfile.pdf";
    $url = "http://knihy.cpress.cz/?p=actions&action=download/file&value=files&id=149253";

    $temp_file_contents = collect_file($url);
    write_to_file($temp_file_contents,$new_file_name)

    // end loop here
?>

from here