Home  >  Article  >  Backend Development  >  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:00:45918browse

How to download remote files to local storage with PHP

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

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";

?>

1 2

3

4 5

67 8 9 10 11 12
13
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"; ?>
I hope this article will be helpful to everyoneโ€™s PHP programming design. http://www.bkjia.com/PHPjc/973112.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/973112.htmlTechArticleHow to download remote files to local storage in PHP This article mainly introduces how to download remote files to local storage in PHP , the example analyzes the operation skills of PHP remote files, with certain reference...
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