Download Remote File to Server with PHP

前端 未结 8 645
刺人心
刺人心 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: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'));
    

提交回复
热议问题