Download Remote File to Server with PHP

前端 未结 8 634
刺人心
刺人心 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:19

    Split it into different stages:

    $raw = file_get_contents($_url);
    ... check if $raw has anything useful in it
    file_put_contents($_dir, $raw);
    ... check if the file showed up
    

    Either the fetch is failing in file_get_contents, or the write is failing in file_put_contents, or the file you're downloading is too large and exceeds your PHP's default memory_limit.

    0 讨论(0)
  • 2020-12-09 12:20

    Use curl to download file from remote server like below.

    $url = "http://path/toserver/filename";
    $destination = "uploads/filename";    
    $fp = fopen ($destination, 'w+');
      $ch = curl_init();
      curl_setopt( $ch, CURLOPT_URL, $url );
      curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
      curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
      curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
    
      curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
      curl_setopt( $ch, CURLOPT_FILE, $fp );
      curl_exec( $ch );
      curl_close( $ch );
      fclose( $fp );
    

    reference http://www.tricksofit.com/2014/04/download-file-from-remote-server-in-php

    0 讨论(0)
  • 2020-12-09 12:21

    With validations...

    Validate if file exists first:

    function doesUrlExists($url) {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
        if($code == 200){
            $status = true;
        }else{
            $status = false;
        }
        curl_close($ch);
        return $status;
    }
    

    And then put file content (with laravel storage class):

     if(!doesUrlExists($url_file)) {
         die('The remote file is not accessible. Please check the URL.');
     }
    
     Storage::disk('local')
              ->put($file_destintation, fopen($url_file, 'r'));
    
    0 讨论(0)
  • 2020-12-09 12:25

    Since PHP 5.1.0, file_put_contents() supports writing piece-by-piece by passing a stream-handle as the $data parameter:
    No need to use Curl

    file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
    
    0 讨论(0)
  • 2020-12-09 12:34

    You may not have fopen wrappers enabled so file_get_contents may not work. Try using curl or a library like Snoopy.

    0 讨论(0)
  • 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.
    
    0 讨论(0)
提交回复
热议问题