Home >Backend Development >PHP Tutorial >How to download remote files with PHP
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 '<pre class="brush:php;toolbar:false"><img src="'.$img.'">'; 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!