Petey Howell
Petey Howell

Reputation: 189

Download file to server PHP

The file I am trying to download is located here

https://assetgame.roblox.com/Asset/?id=942531514

When you load the link, it auto forces a download on you.

I'm trying to find a way to make PHP download the file and store it on the server for later use.

I've tried Curl and file_put_contents of file_get_contents, but neither have worked.

Thanks!

Upvotes: 0

Views: 507

Answers (2)

hanshenrik
hanshenrik

Reputation: 21675

your url use a HTTP Location Redirect, which file_get_contents does not understand at all, which is why file_get_contents definitely won't work (warning, another reason file_get_contents might fail is because of the allow_url_fopen php.ini setting). curl does understand it, but ignores it by default, that's probably why your curl code failed. explicitly tell curl to follow http redirects with the CURLOPT_FOLLOWLOCATIONoption, and it should work with curl, eg

$ch = curl_init ( 'https://assetgame.roblox.com/Asset/?id=942531514' );
curl_setopt_array ( $ch, array (
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_RETURNTRANSFER => true
) );
$xml=curl_exec($ch);
var_dump($xml);

Upvotes: 1

Harman Kamboj
Harman Kamboj

Reputation: 437

Here's the breakdown.

The url you are trying to call : https://assetgame.roblox.com/Asset/?id=942531514

The url the file is located at: http://c3.rbxcdn.com/43865ade81ebad6d3493166f3bcfc631

Since the server is returning a forced download in the header. You can try something like this:

function download_file($url,$saveto){
    $cinit = curl_init ($url);
    curl_setopt($cinit, CURLOPT_HEADER, 0);
    curl_setopt($cinit, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($cinit, CURLOPT_BINARYTRANSFER,1);
    $cexec=curl_exec($cint);
    curl_close ($cinit);
    if(file_exists($saveto)){
        unlink($saveto);
    }
    $fp = fopen($saveto,'x');
    fwrite($fp, $raw);
    fclose($fp);
}

Hope this works for you.

Upvotes: 0

Related Questions