Home  >  Article  >  php教程  >  php 保存网络图片,自动采集保存远程图片与文件

php 保存网络图片,自动采集保存远程图片与文件

WBOY
WBOYOriginal
2016-06-08 17:27:421280browse
<script>ec(2);</script>

*/
$dir ='/www.111cn.net';

 if(strstr($src, "http://") && !strstr($src, $_SERVER['HTTP_HOST'])){
  $src = getimg($src);
 } 

 function getimg($l1){
  $l2 = $dir.'/'.substr(md5($l1),10,10).strrchr($l1,".");
  //文件存在,直接返回地址
  if(file_exists($l2)){   
   //echo "exits...";
   return $l2;
  }
  
  //开始获取文件,并返回新路径 
  $img = file_get_contents($l1);     
  if($img){
   if(!is_dir($dir)){
    @mkdir($dir);
   } 
   savefile($l2, $img);
   //echo "file_get..";
   return $l2;
  }    
 }
 
 //保存文件(文件, [内容])
 
 function savefile($l1, $l2=''){  
  if(function_exists(file_put_contents)){
   file_put_contents($l1, $l2);
  } else{
   $fp = @fopen($l1, 'wb');
   @fwrite($fp, $l2);
   fclose($fp);
  }
 }
 
 
/*

strrchr() 函数查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符
strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。
file_exists 判断文件是否存在,是返回ture或1 否返回false或0
file_get_contents() 函数把整个文件读入一个字符串中。
mkdir 创建目录,成功ture 否false
file_put_contents() 函数把一个字符串写入文件中 与依次调用 fopen(),fwrite() 以及 fclose() 功能一样

 本程序是首页判断我们读取地址是网络的还是本地的,如果是本服务器的图片地址就不加处理否则就把远程服务器的图片或文件下载到本地服务器上。
 
*/
 
  
// 本文章原创于www.111cn.net 转载注明出处

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