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
Release: 2016-07-13 10:00:45
Original
974 people have browsed it

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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template