2012-03-30 79 views
-1

我必須從網絡源下載與數據相關的pdf文件。我知道文件的完整路徑。我已經嘗試過使用curl,但花了很長時間並寫了一個0字節的文件。如何使用php下載文件,提供的路徑已知

$ch = curl_init ($url); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); 
$rawdata = curl_exec($ch); 
curl_close ($ch); 
if(file_exists($fullpath)){ 
    unlink($fullpath); 
} 
$fp = fopen($fullpath,'x'); 
fwrite($fp, $rawdata); 
fclose($fp); 
+1

請發表您的代碼。 – Shoban 2012-03-30 04:35:31

+0

發佈您的代碼。 – Nilpo 2012-03-30 04:36:12

+1

[你有什麼試過](http://mattgemmell.com/2008/12/08/what-have-you-tried/)? – ghoti 2012-03-30 04:36:41

回答

1
$ch = curl_init("http://www.example.com/"); 
$fp = fopen("example_homepage.txt", "w"); 

curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_HEADER, 0); 

curl_exec($ch); 
curl_close($ch); 
fclose($fp); 

http://www.php.net/manual/en/curl.examples-basic.php

或此(如果fopen封裝都設置在你的PHP的conf):

$file = 'http://somehosted.com/file.pdf'; // URL to the file 

$contents = file_get_contents($file); // read the remote file 

touch('somelocal.pdf'); // create a local EMPTY copy 

file_put_contents('somelocal.pdf', $contents); // put the fetchted data into the newly created file 

// done :) 

這一次也許適合你最好:http://www.jonasjohn.de/snippets/php/curl-example.htm

相關問題