Home  >  Article  >  Backend Development  >  How to download remote files with PHP

How to download remote files with PHP

墨辰丷
墨辰丷Original
2018-06-12 09:57:073659browse

This article mainly introduces the method of PHP downloading remote files to local storage. It analyzes the operation skills of PHP remote files with examples. It has certain reference value. Friends in need can refer to the following

Examples of this article How to download remote files to local storage in PHP. The specific implementation method is as follows:

<?php
function GrabImage($url,$filename="") {
  if($url=="") return false;
  if($filename=="") {
    $ext=strrchr($url,".");
    if($ext!=".gif" && $ext!=".jpg") return false;
    $filename=date("dMYHis").$ext;
  }
  ob_start();
  readfile($url);
  $img = ob_get_contents();
  ob_end_clean();
  $size = strlen($img);
  $fp2=@fopen($filename, "a");
  fwrite($fp2,$img);
  fclose($fp2);
  return $filename;
}
function gethttpimage($url){ 
  if(!empty($url)){ 
    $filename=uniqid().strrchr($url,"."); 
    echo $filename;
    $get_file=@file_get_contents($url); 
    if($get_file){ 
      $fp=@fopen($filename,"w"); 
      @fwrite($fp,$get_file); 
      @fclose($fp); 
    } 
    return $imgUrl; 
  }else{ 
    return false; 
  } 
} 
//$img=GrabImage("//www.jb51.net/images/logo.gif","");
$img=gethttpimage("//www.jb51.net/images/logo.gif","");
if($img) echo &#39;<pre class="brush:php;toolbar:false"><img src="&#39;.$img.&#39;">
'; else echo "false"; ?>

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related recommendations:

How to operate uploaded files and images using php

php automatically adjusts the font size in a limited area Function

php method to implement array sorting based on ArraySortUtil

The above is the detailed content of How to download remote files with PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn