2011-06-20 13 views
73

का उपयोग कर एक बड़ी फ़ाइल डाउनलोड करना मुझे कर्ल का उपयोग करके दूरस्थ फ़ाइल डाउनलोड करने की आवश्यकता है।कर्ल

यहाँ मैं नमूना कोड:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$st = curl_exec($ch); 
$fd = fopen($tmp_name, 'w'); 
fwrite($fd, $st); 
fclose($fd); 

curl_close($ch); 

लेकिन यह बड़ी फ़ाइलों को संभाल नहीं कर सकते, क्योंकि यह पहली स्मृति में पढ़ता है।

क्या फ़ाइल को सीधे डिस्क पर स्ट्रीम करना संभव है?

उत्तर

139
<?php 
set_time_limit(0); 
//This is the file where we save the information 
$fp = fopen (dirname(__FILE__) . '/localfile.tmp', 'w+'); 
//Here is the file we are downloading, replace spaces with %20 
$ch = curl_init(str_replace(" ","%20",$url)); 
curl_setopt($ch, CURLOPT_TIMEOUT, 50); 
// write curl response to file 
curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
// get curl response 
curl_exec($ch); 
curl_close($ch); 
fclose($fp); 
?> 
+4

अपनी टिप्पणी @ yes123 की रक्षा करें, मुझे जानना रूचि है। – Michelle

+7

अगर मैं गलत हूं, तो मुझे सही करें, लेकिन मुझे नहीं लगता कि आपको वास्तव में 'CURLOPT_FILE' का उपयोग कर रहे डेटा को मैन्युअल रूप से' fwrite 'करने की आवश्यकता है। –

+1

जैसा कि @SashaChedygov ने ऊपर बताया है, आपको 'fwrite' और 'CURLOPT_FILE' का उपयोग करने की आवश्यकता नहीं है। '$ एफपी' पास करना पर्याप्त है। मैंने फाइल में सामग्री के अंत में दोनों को किया और '1' के साथ समाप्त हो गया। – paperclip

20

मैं इस आसान समारोह का उपयोग करें:

यह पूर्ण नहीं होगा अपनी स्मृति एक 4094 बाइट कदम के साथ यह डाउनलोड करके

function download($file_source, $file_target) { 
    $rh = fopen($file_source, 'rb'); 
    $wh = fopen($file_target, 'w+b'); 
    if (!$rh || !$wh) { 
     return false; 
    } 

    while (!feof($rh)) { 
     if (fwrite($wh, fread($rh, 4096)) === FALSE) { 
      return false; 
     } 
     echo ' '; 
     flush(); 
    } 

    fclose($rh); 
    fclose($wh); 

    return true; 
} 

उपयोग:

 $result = download('http://url','path/local/file'); 

फिर आप अगर सब कुछ जाँच कर सकते हैं ठीक है:

 if (!$result) 
     throw new Exception('Download error...'); 
+6

thx लेकिन मुझे कर्ल के साथ की आवश्यकता है :) – kusanagi

+1

मैं http त्रुटि कैसे पकड़ूंगा और इसे टाइमआउट कैसे करूं? – Michelle

+1

@ सेवरस आप http त्रुटि को 'fopen()' के रूप में पकड़ते हैं क्योंकि झूठी और टाइमआउट आप इसे लूप (कॉल 'टाइम()' में रखते हैं और गणित करते हैं) –

5

यदि आप निर्दिष्ट यूआरएल की सामग्री डाउनलोड करना चाहते हैं तो नीचे दिए गए कोड को भी फ़ाइल में सहेजना चाहते हैं।

<?php 
$ch = curl_init(); 
/** 
* Set the URL of the page or file to download. 
*/ 
curl_setopt($ch, CURLOPT_URL,'http://news.google.com/news?hl=en&topic=t&output=rss'); 

$fp = fopen('rss.xml', 'w+'); 
/** 
* Ask cURL to write the contents to a file 
*/ 
curl_setopt($ch, CURLOPT_FILE, $fp); 

curl_exec ($ch); 

curl_close ($ch); 
fclose($fp); 
?> 

यदि आप FTP सर्वर से फ़ाइल डाउनलोड करना चाहते हैं तो आप php FTP एक्सटेंशन का उपयोग कर सकते हैं। नीचे दिए गए कोड को खोजने करें:

<?php 
$SERVER_ADDRESS=""; 
$SERVER_USERNAME=""; 
$SERVER_PASSWORD=""; 
$conn_id = ftp_connect($SERVER_ADDRESS); 

// login with username and password 
$login_result = ftp_login($conn_id, $SERVER_USERNAME, $SERVER_PASSWORD); 

$server_file="test.pdf" //FTP server file path 
$local_file = "new.pdf"; //Local server file path 

##----- DOWNLOAD $SERVER_FILE AND SAVE TO $LOCAL_FILE--------## 
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) { 
    echo "Successfully written to $local_file\n"; 
} else { 
    echo "There was a problem\n"; 
} 

ftp_close($conn_id); 
?> 
2

जब curl एक बड़ी फ़ाइल डाउनलोड करने के लिए प्रयोग किया जाता है तो CURLOPT_TIMEOUT मुख्य विकल्प आपके लिए सेट करने के लिए है।

CURLOPT_RETURNTRANSFER मामले आप पीडीएफ/CSV/छवि की तरह फ़ाइल हो रही है में सच हो गया है आदि

आप यहाँ से अधिक अधिक विस्तार मिल सकता है (सही यूआरएल) Curl Doc

कि पृष्ठ से:

curl_setopt($request, CURLOPT_TIMEOUT, 300); //set timeout to 5 mins 

curl_setopt($request, CURLOPT_RETURNTRANSFER, true); // true to get the output as string otherwise false 
+0

आप कर्ल [समझ कर्ल मूल बातें] के साथ फ़ाइल डाउनलोड के संबंध में ब्लॉग उदाहरण के माध्यम से भी जा सकते हैं (http://prashantpandeytech.blogspot.in /2012/10/there-is-common-problem-when-large-site.html) –

+0

गलत यूआरएल। यह कुछ और लोड करता है। – user427969

1

आप इस समारोह है, जो फाइल सिस्टम में एक tempfile बनाता है और डाउनलोड की गई फ़ाइल के लिए पथ लौटाता है यदि सब कुछ ठीक काम किया उपयोग कर सकते हैं:

function getFileContents($url) 
{ 
    // Workaround: Save temp file 
    $img = tempnam(sys_get_temp_dir(), 'pdf-'); 
    $img .= '.' . pathinfo($url, PATHINFO_EXTENSION); 

    $fp = fopen($img, 'w+'); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 

    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_FILE, $fp); 
    curl_setopt($ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 

    $result = curl_exec($ch); 
    curl_close($ch); 

    fclose($fp); 

    return $result ? $img : false; 
}