Download Remote File to Server with PHP

前端 未结 8 683
刺人心
刺人心 2020-12-09 12:10

I\'ve been looking all over the place for the last two days and trying everything and still can\'t get anything to work. I feel like this should be a relatively simple thin

8条回答
  •  余生分开走
    2020-12-09 12:36

    This should do it :

    set_time_limit(0);
    
    $url = 'http://www.freewarelovers.com/android/download/temp/1306495040_Number_Blink_1.1.1.apk';
    $file = fopen(dirname(__FILE__) . '/downloads/a.apk', 'w+');
    
    $curl = curl_init();
    
    // Update as of PHP 5.4 array() can be written []
    curl_setopt_array($curl, [
        CURLOPT_URL            => $url,
    //  CURLOPT_BINARYTRANSFER => 1, --- No effect from PHP 5.1.3
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FILE           => $file,
        CURLOPT_TIMEOUT        => 50,
        CURLOPT_USERAGENT      => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'
    ]);
    
    $response = curl_exec($curl);
    
    if($response === false) {
        // Update as of PHP 5.3 use of Namespaces Exception() becomes \Exception()
        throw new \Exception('Curl error: ' . curl_error($curl));
    }
    
    $response; // Do something with the response.
    

提交回复
热议问题