Home > Article > Backend Development > How to download remote files to local storage in PHP_PHP tutorial
This article mainly introduces the method of downloading remote files to local storage with PHP, and analyzes the operation skills of PHP remote files with examples. It has certain reference value, friends in need can refer to it
The example in this article describes how PHP downloads remote files to local storage. Share it with everyone for your reference. The specific implementation method is as follows:
?
3 4 513 14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<๐>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.jb51.net/images/logo.gif","");<๐>
<๐>$img=gethttpimage("http://www.jb51.net/images/logo.gif","");<๐>
<๐>if($img) echo '<img src="'.$img.'">'; else echo "false"; ?> |