PHP">
当前位置:Gxlcms > PHP教程 > PHP下载远程文件到本地存储的代码

PHP下载远程文件到本地存储的代码

时间:2021-07-01 10:21:17 帮助过:1人阅读

  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 '
    ';
  36. else echo "false";
  37. ?>

PHP

人气教程排行