Search code examples
phpserverdownloadmymaps

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


I use Google's My maps. The problem is that Google makes many restrictions in using their API, which makes it hard to insert or retrieve data from the map automatically.

I have a direct download link to this map file which downloads the KML file to any device when clicked.

What I want is a code in PHP to open this link, download the KML file and save it automatically to my server.

i have seen this in a different question but it didnt work for me:

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

I think the problem is that my link does not link to a file (does not end in .zip or other file extensions) This is an example for my link: here

Thank you!


Solution

  • You can use file_put_contents and file_get_contents functions, like so:

    $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));