PHP:从url下载(没有直接链接)

时间:2017-05-06 14:08:05

标签: php pdf download

我想从网址下载,但此网址不是直接

我的代码:

$data = $curl->Open("http://yourname.com/file/xyz","")->contents;
print_r($data);

运行此页面后,我收到此错误:

%PDF-1.4 %���� 124 0 obj<> endobj xref 124 23 0000000016 00000 n 0000001527 00000 n 0000000756 00000 n 0000001611 00000 n 0000001744 00000 n 0000001867 00000 n 0000001944 00000 n 0000002191 00000 n 0000002456 00000 n 0000003192 00000 n 0000003699 00000 n 0000004187 00000 n 0000004329 00000 n 0000005166 00000 n 0000005313 00000 n 0000005940 00000 n 0000036025 00000 n 0000036289 00000 n 0000037197 00000 n 0000051990 00000 n 0000056578 00000 n 0000084575 00000 n 0000084845 00000 n trailer <<28b1fe3094706e478100fb6d306bd490>]>> startxref 0 %%EOF 126 0 obj<>stream x�b```"V63A��2�0pt01`^700�Zx��\�� ���y�6=Ptd�&���#x�K�E�:c���G��-B�O��8|�A|A����]܎@�7g'� �dg\*��6�Ҵ�˭���<��z��r$뺚d҂�k|����q�;�h�ܳ�MZ:�҂>�:�|���Gᬢ�g�MK�ӡ#/�.�e�t%�}�S�\�)���ҍ�8BU簥@Lꇞe�6�:�kV]yʠz��tV�li:�J�9�,g�4K�,f߹F�h�֕n���je���=�Ð� ��c��ATl�u����E%?,7N�����b˶��f/�(*��Vx�F�FGGG���;@����08&LlP�L.- ��*�(���g2 J`hwA�f��1�d``�@qB�dѰLY�s�ಬ0Yc�L}h(�Y\aBP�#�T��C�{�G �J !�fB���,[���ˀ�Va�g8����k �`=���#WxZ�+T��Q�d�b�Ty�'�:��5JKO00,YͶ�,?v���v�;z� endstream endobj 125 0 obj<> endobj 127 0 obj<> endobj 128 0 obj<>/ProcSet[/PDF/Text]/ExtGState<>>> endobj 129 0 obj<> endobj 130 0 obj<> endobj 131 0 obj<> endobj 132 0 obj<>stream H�T�O��0���>���I�U�@i���wc�H A${�o_��������{���d���i8�L�m��g���4�I���)����4�x ���?��h�����z����_���*�>/^oӻ���R|�?���e: �s��?~Α�q�-gn&7ۭ�ү�������L�_�G1Vq��/Q��2��U�cn%l������UYQ��ᗟV��M�nEEE��+����PT�ٗ��A�Z3]A݃�9�Z�u�� ��rl*�t����jA�CgU�J�g�f!E��U�z��� 

2 个答案:

答案 0 :(得分:1)

您需要使用//assuming you are trying to download the file and not just open up a `doc` of `pdf` on your site. $file = 'link-to-file'; header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename='.basename($file)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); ,例如文件下载:

ngx_http_geoip_module

答案 1 :(得分:1)

您需要使用标头强制下载。有关更多详细信息,请参阅代码中的注释

<?php

$file = 'http://pdf995.com/samples/pdf.pdf';
download($file);

function download($url) {
    set_time_limit(0); // Prevent timeouts
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $r = curl_exec($ch);
    curl_close($ch);
    header('Content-Type: application/force-download');
    header('Content-Disposition: attachment; filename="' . basename($url) . '"'); // Change the name of the downloaded file if desired
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: ' . strlen($r)); // provide file size
    header('Connection: close');
    echo $r;
}

?>