簡體   English   中英

從直接下載鏈接下載文件並保存到服務器-PHP

[英]Download file from direct download link and save to server - PHP

我使用Google的“我的地圖”。 問題在於Google在使用其API時受到許多限制,這使得自動插入地圖或從地圖檢索數據變得困難。

我有此地圖文件的直接下載鏈接,單擊該鏈接即可將KML文件下載到任何設備。

我想要的是PHP中的代碼以打開此鏈接,下載KML文件並將其自動保存到我的服務器中。

我在另一個問題中看到了這一點,但對我沒有用:

shell_exec("wget -P /target/directory/ http://download.link.com/download.zip");

我認為問題是我的鏈接未鏈接到文件(不以.zip或其他文件擴展名結尾)這是我的鏈接的示例: 此處

謝謝!

這是使用cURL將信息保存到文件中的一種解決方案

 <?php

$ch = curl_init();

// You can add here the URL contains the information which should be fetched
curl_setopt($ch, CURLOPT_URL,'https://www.google.com/maps/d/kml?mid=1FcgdJK9tLMt_dygTbxObJHjyCoo&forcekml=1&cid=mp&cv=qDJ98FtrON4.he.');

// You can define the name which will be created with the extension, in this case it's .KML
$openFile = fopen('file.KML', 'w+');

// Here Curl will save the fetched information into the file.
curl_setopt($ch, CURLOPT_FILE, $openFile);

curl_exec ($ch);

curl_close ($ch);

fclose($openFile);

確保您對將在其中創建文件的服務器上的文件夾授予權限。

您可以使用file_put_contentsfile_get_contents函數,如下所示:

$path = "/target/directory/file.kml"; // Where the file should be saved to
$link = "https://www.google.com/maps/d/u/0/kml..."; // Download link
file_put_contents($path, file_get_contents($link));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM