查找下面的代碼,如果你想下載指定的URL的內容也想保存到一個文件。
<?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);
?>
捍衛你的評論@ yes123,我很想知道。 – Michelle
糾正我,如果我錯了,但我不認爲你實際上需要手動'fwrite'數據,因爲你使用'CURLOPT_FILE'。 –
正如@SashaChedygov在上面指出的,你不需要使用'fwrite'和'CURLOPT_FILE'。傳遞'$ fp'就足夠了。我做了兩個,最後在文件內容的末尾加上了'1'。 – paperclip