Home >Backend Development >PHP Tutorial >How to download remote files to local storage in PHP, _PHP tutorial

How to download remote files to local storage in PHP, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:01:06792browse

How to download remote files to local storage with PHP,

The example in this article describes the method of downloading remote files to local storage with PHP. Share it with everyone for your reference. The specific implementation method is as follows:

<&#63;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("http://www.bkjia.com/images/logo.gif","");
$img=gethttpimage("http://www.bkjia.com/images/logo.gif","");
if($img) echo '<pre class="brush:php;toolbar:false"><img src="'.$img.'">
'; else echo "false"; ?>

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/972639.htmlTechArticleHow to download remote files to local storage in PHP. This article describes the method of downloading remote files to local storage in PHP. Share it with everyone for your reference. The specific implementation method is as follows: p...
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