2017-05-06 43 views
0

我想從以下網址下載,但這個網址不是直接PHP:從網址下載(沒有直接的聯繫)

我的代碼:

$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��� 
+1

這看起來是一個PDF二進制文件。那是你正在尋找的文件嗎? – Halcyon

+0

_你是什麼意思_「這個URL不直接」_? –

+0

@Halcyon我想下載文件到我的服務器 – Mohammad

回答

1

你需要使用headers,例如文件下載:

//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); 
+0

$ file ='http://yourname.com/file/xyz';不爲我工作 – Mohammad

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; 
} 

?> 
+0

我的網址沒有.PDF擴展名格式 – Mohammad

+0

沒關係,這只是一個例子。如果http://example.com/files/file路由到文件,那麼它仍然會下載。如果您試圖將文件保存到您的服務器,那麼您將需要編寫文件而不是回顯它,但這是與實際下載相關的部分。 –

+0

我無法找到文件的路徑路徑,我只有這個網址:'http://yourname.com/file/xyz' – Mohammad