Home  >  Article  >  Backend Development  >  PHP code to download remote files to local storage

PHP code to download remote files to local storage

WBOY
WBOYOriginal
2016-07-25 08:43:221008browse
  1. function GrabImage($url,$filename="") {
  2. if($url=="") return false;
  3. if($filename=="") {
  4. $ext=strrchr($url,".");
  5. if($ext!=".gif" && $ext!=".jpg") return false;
  6. $filename=date("dMYHis").$ext;
  7. }
  8. ob_start();
  9. readfile($url);
  10. $img = ob_get_contents();
  11. ob_end_clean();
  12. $size = strlen($img);
  13. $fp2=@fopen($filename, "a");
  14. fwrite($fp2,$img);
  15. fclose($fp2);
  16. return $filename;
  17. }
  18. function gethttpimage($url){
  19. if(!empty($url)){
  20. $filename=uniqid().strrchr($url,".");
  21. echo $filename;
  22. $get_file=@file_get_contents($url);
  23. if($get_file){
  24. $fp=@fopen($filename,"w");
  25. @fwrite($fp,$get_file);
  26. @fclose($fp);
  27. }
  28. return $imgUrl;
  29. }else{
  30. return false;
  31. }
  32. }
  33. //$img=GrabImage("http://img.it-home.org/data/attachment/forum/2016pic1/error_link.gif","");
  34. $img=gethttpimage("http://img.it-home.org/data/attachment/forum/2016pic1/error_link.gif","");
  35. if($img) echo '
    <img src="'.$img.'">
    ';
  36. else echo "false";
  37. ?>
复制代码

PHP


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
Previous article:php zip compression codeNext article:php zip compression code