dedecms 下载远程图片和资源失败解析方法

 更新时间:2016年11月25日 15:58  点击:2084

1.远程图片本地化要求空间相应目录可写外还要求php教程的allow_url_fopen 选项设置为On


这些是远程图片本地化的必要条件,但并不是说有了这些就一定可以远程图片本地化了,这个与对方的服务器和网络环境整体情况都有很大的关系(对php来说是不可控的),

 

2.DEDECMS里面保存远程图片的函数


/dede/inc/inc_archives_functions.php   主要是获得图片保存的

/include/pub_collection_functions.php  不能直接保存下来的,或者有简单防盗链的。造假Referer。

 

 

AdodbZip1.1有更新,更新了下载地址。

 


SmartyZip,一个Smarty的装载类
<?php教程

// 设定参数
SmartyZip::$zip_url = 'http://www.smarty.net教程/distributions/Smarty-2.6.26.zip'; //[设置项]Smarty的Zip文件下载地址
SmartyZip::$zip_file = sys_get_temp_dir () . preg_replace ( '/^.*/(Smarty-.*.zip)$/i', 'smarty/$1', SmartyZip::$zip_url ); //[设置项]Smarty的Zip文件缓存位置
SmartyZip::$entry_dir = preg_replace ( '/^.*/(Smarty-.*).zip$/i', '$1/libs', SmartyZip::$zip_file );
SmartyZip::$extract_dir = sys_get_temp_dir () . 'smarty/' . SmartyZip::$entry_dir; //[设置项]Smarty程序文件缓存位置
SmartyZip::$template_dir = dirname ( realpath ( __FILE__ ) ); //[设置项]Smarty模板文件 所在位置 
SmartyZip::$compile_dir = sys_get_temp_dir () . 'smarty/template_c/' . md5 ( SmartyZip::$template_dir ); //[设置项]Smarty编译文件缓存位置


// 注册协议
if (! in_array ( 'SmartyZip', stream_get_wrappers () )) {
 stream_wrapper_register ( 'SmartyZip', 'SmartyZip' );
}
// 定义常量
if (! defined ( 'SMARTY_DIR' )) {
 define ( 'SMARTY_DIR', 'SmartyZip://' );
}
// 包含程序
require_once (SMARTY_DIR . 'Smarty.class.php');
// $smarty = SmartyZip::init(new Smarty); // [选择项]引用即定义$smarty
// return SmartyZip::init(new Smarty); // [选择项]引用即返回$smarty,注意只可引用一次。


/**
 * SmartyZip类定义
 */
class SmartyZip {
 
 /**
  * Smarty变量
  */
 public static $zip_url;
 public static $zip_file;
 public static $entry_dir;
 public static $extract_dir;
 public static $template_dir;
 public static $compile_dir;
 
 /**
  * Stream变量
  */
 private $handle;
 public $context;
 
 /**
  * Smarty函数组
  */
 
 /**
  * init
  * @param Smarty &$smarty
  * @return Smarty
  */
 public static function init(&$smarty) {
  $smarty->template_dir = self::$template_dir;
  if (! is_dir ( self::$compile_dir )) {
   if (mkdir ( self::$compile_dir, 0777, true ) === false) {
    header ( 'Content-type: text/html;charset=utf-8' );
    die ( '请创建目录 ' . self::$compile_dir );
   }
  }
  $smarty->compile_dir = self::$compile_dir;
  return $smarty;
 }
 
 /**
  * Stream函数组
  */
 
 /**
  * __construct
  */
 public function __construct() {
 }
 
 /**
  * stream_cast
  * @param int $cast_as
  * @return resource
  */
 public function stream_cast($cast_as) {
  return false;
 }
 
 /**
  * stream_close
  */
 public function stream_close() {
  fclose ( $this->handle );
 }
 
 /**
  * stream_eof
  * @return bool
  */
 public function stream_eof() {
  return feof ( $this->handle );
 }
 
 /**
  * stream_flush
  * @return bool
  */
 public function stream_flush() {
  return fflush ( $this->handle );
 }
 
 /**
  * stream_lock
  * @param mode $options
  * @return bool
  */
 public function stream_lock($options) {
  return flock ( $this->handle, $options );
 }
 
 /**
  * stream_open
  * @param string $path
  * @param string $mode
  * @param int $options
  * @param string &$opend_path
  * @return bool
  */
 public function stream_open($path, $mode, $options, &$opend_path) {
  // 验证文件地址
  if (! preg_match ( '/^.*?://(.*)$/', $path, $matches )) {
   return false;
  }
  $tmp_file = self::$extract_dir . DIRECTORY_SEPARATOR . $matches [1];
  $entry_file = self::$entry_dir . '/' . str_replace ( '\', '/', $matches [1] );
  $zip_file = self::$zip_file;
  // 验证程序文件
  if (! file_exists ( $tmp_file ) || file_exists ( $zip_file ) && filectime ( $tmp_file ) < filectime ( $zip_file )) {
   // 下载文件
   if (! file_exists ( $zip_file )) {
    // 目录处理
    if (! is_dir ( dirname ( self::$zip_file ) )) {
     if (mkdir ( dirname ( self::$zip_file ), 0777, true ) === false) {
      header ( 'Content-type: text/html;charset=utf-8' );
      die ( '请创建目录 ' . $zip_dir );
     }
    }
    // 下载文件
    if (! file_exists ( self::$zip_file )) {
     $break = true;
     do {
      $url_arr = parse_url ( self::$zip_url );
      $fp = fsockopen ( $url_arr ['host'], isset ( $url_arr ['port'] ) ? ( int ) $url_arr ['port'] : 80, $errno, $errstr, 10 );
      if ($fp === false) {
       break;
      }
      $out = "GET " . $url_arr ['path'] . " HTTP/1.0rnHost: " . $url_arr ['host'] . " rnConnection: closernrn";
      fputs ( $fp, $out );
      if (feof ( $fp )) {
       break;
      }
      $buffer = fgets ( $fp, 1024 );
      if (! preg_match ( '/^HTTP/1.d 200 /i', $buffer )) {
       break;
      }
      $content_length = false;
      $content_start = false;
      while ( ! feof ( $fp ) ) {
       $buffer = fgets ( $fp, 1024 );
       if ($buffer === "rn") {
        $content_start = true;
        break;
       }
       if (preg_match ( '/^Content-Length:s*(d+)/i', $buffer, $matches )) {
        $content_length = ( int ) $matches [1];
       }
      }
      if ($content_length === false || $content_start === false) {
       break;
      }
      $content = stream_get_contents ( $fp );
      if ($content === false) {
       break;
      }
      $result = file_put_contents ( self::$zip_file, $content );
      unset ( $content );
      if ($result === false) {
       break;
      }
      fclose ( $fp );
     } while ( $break = false );
     if ($break) {
      header ( 'Content-type: text/html;charset=utf-8' );
      die ( '请下载文件 <a href="' . self::$zip_url . '">' . self::$zip_url . '</a > 保存为 ' . self::$zip_file );
     }
    }
   }
   // 创建目录
   $tmp_dir = dirname ( $tmp_file );
   if (! is_dir ( $tmp_dir )) {
    if (mkdir ( $tmp_dir, 0777, true ) === false) {
     header ( 'Content-type: text/html;charset=utf-8' );
     die ( '请创建目录 ' . $tmp_dir );
    }
   }
   // 打开压缩文件
   $zip = zip_open ( $zip_file );
   if (! is_resource ( $zip )) {
    return false;
   }
   // 寻找解压文件
   do {
    $entry = zip_read ( $zip );
    if (! is_resource ( $entry )) {
     return false;
    }
    if (zip_entry_name ( $entry ) == $entry_file) {
     break;
    }
   } while ( true );
   // 转存压缩文件
   zip_entry_open ( $zip, $entry );
   file_put_contents ( $tmp_file, zip_entry_read ( $entry, zip_entry_filesize ( $entry ) ) );
   zip_entry_close ( $entry );
   zip_close ( $zip );
  }
  // 打开文件
  $this->handle = fopen ( $tmp_file, $mode );
  if (! is_resource ( $this->handle )) {
   return false;
  }
  return true;
 }
 
 /**
  * stream_read
  * @param int $count
  * @return string
  */
 public function stream_read($count) {
  return fread ( $this->handle, $count );
 }
 
 /**
  * stream_seek
  * @param int $offset
  * @param int $whence=SEEK_SET
  * @return bool
  */
 public function stream_seek($offset, $whence = SEEK_SET) {
  return fseek ( $this->handle, $offset, $whence );
 }
 
 /**
  * stream_set_option
  * @param int $option
  * @param int $arg1
  * @param int $arg2
  * @return bool
  */
 public function stream_set_option($option, $arg1, $arg2) {
  return false;
 }
 
 /**
  * stream_stat
  * @return array
  */
 public function stream_stat() {
  return fstat ( $this->handle );
 }
 
 /**
  * stream_tell
  * @return int
  */
 public function stream_tell() {
  return ftell ( $this->handle );
 }
 
 /**
  * stream_write
  * @param string $data
  * @return int
  */
 public function stream_write($data) {
  return fwrite ( $this->handle, $data );
 }
 
 /**
  * url_stat
  * @param string $path
  * @param int $flag
  * @return array
  */
 public function url_stat($path, $flag) {
  if (! preg_match ( '/^.*?://(.*)$/', $path, $matches )) {
   return false;
  }
  $tmp_file = self::$extract_dir . DIRECTORY_SEPARATOR . $matches [1];
  if (file_exists ( $tmp_file )) {
   if ($flag & STREAM_URL_STAT_LINK) {
    return lstat ( $tmp_file );
   } else {
    return stat ( $tmp_file );
   }
  }
  if ($flag & STREAM_URL_STAT_QUIET) {
   $file_stat = lstat ( __FILE__ );
   $mode = $file_stat ['mode'];
   // smarty plugins adjust
   if (strpos ( $matches [1], 'plugins' . DIRECTORY_SEPARATOR . 'compiler.' ) !== false) {
    if (strpos ( $matches [1], 'plugins' . DIRECTORY_SEPARATOR . 'compiler.assign.php' ) === false) {
     $mode = 0;
    }
   } elseif (strpos ( $matches [1], 'plugins' . DIRECTORY_SEPARATOR . 'block.' ) !== false) {
    if (strpos ( $matches [1], 'plugins' . DIRECTORY_SEPARATOR . 'block.textformat.php' ) === false) {
     $mode = 0;
    }
   }
   $arr = array ('dev' => 0, 'ino' => 0, 'mode' => $mode, 'nlink' => 0, 'uid' => 0, 'gid' => 0, 'rdev' => 0, 'size' => 0, 'atime' => 0, 'mtime' => 0, 'ctime' => 0, 'blksize' => 0, 'blocks' => 0 );
   return array_merge ( array_values ( $arr ), $arr );
  }
  return false;
 }
}
?>

使用Smarty很简单:

1. 包含SmartyZip文件。
2. 使用SmartyZip::init(new Smarty)自动为smarty对象设置默认路径。

样例代码:view sourceprint?1 <?php 

2 include_once 'SmartyZip.php'; 

3 $smarty = SmartyZip::init(new Smarty); 

4 $smarty->assign ( 'name', 'SmartyZip' ); 

5 $smarty->display ( 'test.<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=html">html</SPAN>' ); 

6 ?>

$sourcecode=getsourcecode("http://www.sogou.com/web?query=".$q);
//echo ":".$sourcecode;
if($sourcecode){
$sourcecode=iconv('gb2312','utf-8',$sourcecode);
 preg_match('/搜狗评级:(.*?)&nbsp;/', $sourcecode, $cmatche);
 if($cmatche[1]){
  echo '<script type="text/javascript教程">parent.document.getElementById("sogourank").innerHTML = "'.$cmatche[1].'";</script>'." "; 
 }else{
  echo '<script type="text/javascript">parent.document.getElementById("sogourank").innerHTML = "0/100";</script>'." "; 
 }
}

$q=strtolower(trim($_GET['q']));
$q=matchdomain($q);
if($q==''){exit;}
function matchdomain($q){
preg_match('/(([a-z0-9]w{0,61}?[a-z0-9]|[a-zA-Z0-9]).){1,}(aero|arpa|asia|biz|cat|com|coop|co|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|[a-z][a-z])(.[a-z][a-z]){0,1}/', $q, $matches);
return $matches[0];
}
for($i=0;$i<5;$i++){
$pagecode = @file_get_contents("http://www.baidu.com/s?rn=1&wd=site:".$q);
    if($pagecode){
 break;
 }
}
    if($pagecode){
  preg_match('/百度一下,找到相关网页(约)?(.*?)篇,用时/', $pagecode, $baidus);
  if($baidus[2]){
  $sitebaidu=$baidus[2];
  }else{
  $sitebaidu=0;
  }
  echo "<script type="text/javascript教程">parent.document.getElementById("sitebaidu").innerHTML = "<a href=http://www.baidu.com/s?wd=site:".$q." target=_blank>".$sitebaidu."</a>";</script> ";
  echo "<script type="text/javascript">parent.document.getElementById("sitebaidu2").innerHTML = "<a href=http://www.baidu.com/s?wd=site:".$q." target=_blank>".$sitebaidu."</a>";</script> ";
 }else{
  $sitebaidu=-1;
  echo "<script type="text/javascript">parent.document.getElementById("sitebaidu").innerHTML = "<a href=indexed/rcsitebaidu.php教程?q=:".$q." target=recheck>u91cdu67e5</a>";</script> ";
  echo "<script type="text/javascript">parent.document.getElementById("sitebaidu2").innerHTML = "<a href=indexed/rcsitebaidu.php?q=:".$q." target=recheck>u91cdu67e5</a>";</script> ";
 }

require_once $_SERVER['DOCUMENT_ROOT'].'/inc/conn.php';

 $sitebaidu=(int)str_replace(',','',$sitebaidu);
 $domaindate=$q.'|'.date('Y-m-d',time());
 $insertsql="UPDATE c_indexed sitebaidu='$sitebaidu' where domaindate='$domaindate'";
 $result=mysql教程_query($insertsql);
 //or die(mysql_error());

for($i=0;$i<5;$i++){
$pagecode = @file_get_contents("http://www.youdao.com/search?q=site:".$q);
    if($pagecode){
 break;
 }
}
//echo $pagecode;
    if($pagecode){
  preg_match('/<!-- RESULT_NO=(.*?) DURATION=/', $pagecode, $youdaos);
  if($youdaos[1]){
  $siteyoudao=$youdaos[1];
  }else{
  $siteyoudao=0;
  }
  echo "<script type="text/javascript教程">document.getElementById("siteyoudao").innerHTML = "<a href=http://www.youdao.com/search?q=site:".$q." target=_blank>".$siteyoudao."</a>";</script> "; 
 }else{
  $siteyoudao=-1;
  echo "<script type="text/javascript">document.getElementById("siteyoudao").innerHTML = "<a href=indexed/rcsiteyoudao.php教程?q=".$q." target=recheck>u91cdu67e5</a>";</script> "; 
 }
 
for($i=0;$i<5;$i++){
$pagecode = @file_get_contents("http://www.youdao.com/search?q=inlink:".$q);
    if($pagecode){
 break;
 }
}
    if($pagecode){
  preg_match('/<!-- RESULT_NO=(.*?) DURATION=/', $pagecode, $youdaol);
  if($youdaol[1]){
  $linkyoudao=$youdaol[1];
  }else{
  $linkyoudao=0;
  }
  echo "<script type="text/javascript">document.getElementById("linkyoudao").innerHTML = "<a href=http://www.youdao.com/search?q=inlink:".$q." target=_blank>".$linkyoudao."</a>";</script> "; 
 }else{
  $linkyoudao=-1;
  echo "<script type="text/javascript">document.getElementById("linkyoudao").innerHTML = "<a href=indexed/rclinkyoudao.php?q=".$q." target=recheck>u91cdu67e5</a>";</script> "; 
 }

[!--infotagslink--]

相关文章

  • 使用PHP+JavaScript将HTML页面转换为图片的实例分享

    这篇文章主要介绍了使用PHP+JavaScript将HTML元素转换为图片的实例分享,文后结果的截图只能体现出替换的字体,也不能说将静态页面转为图片可以加快加载,只是这种做法比较interesting XD需要的朋友可以参考下...2016-04-19
  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • Photoshop古装美女图片转为工笔画效果制作教程

    今天小编在这里就来给各位Photoshop的这一款软件的使用者们来说说把古装美女图片转为细腻的工笔画效果的制作教程,各位想知道方法的使用者们,那么下面就快来跟着小编一...2016-09-14
  • Python 图片转数组,二进制互转操作

    这篇文章主要介绍了Python 图片转数组,二进制互转操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-09
  • php抓取网站图片并保存的实现方法

    php如何实现抓取网页图片,相较于手动的粘贴复制,使用小程序要方便快捷多了,喜欢编程的人总会喜欢制作一些简单有用的小软件,最近就参考了网上一个php抓取图片代码,封装了一个php远程抓取图片的类,测试了一下,效果还不错分享...2015-10-30
  • C#实现HTTP下载文件的方法

    这篇文章主要介绍了C#实现HTTP下载文件的方法,包括了HTTP通信的创建、本地文件的写入等,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮

    jquery左右滚动焦点图banner图片鼠标经过显示上下页按钮...2013-10-13
  • 利用JS实现点击按钮后图片自动切换的简单方法

    下面小编就为大家带来一篇利用JS实现点击按钮后图片自动切换的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2016-10-25
  • Photoshop枪战电影海报图片制作教程

    Photoshop的这一款软件小编相信很多的人都已经是使用过了吧,那么今天小编在这里就给大家带来了用Photoshop软件制作枪战电影海报的教程,想知道制作步骤的玩家们,那么下面...2016-09-14
  • js实现上传图片及时预览

    这篇文章主要为大家详细介绍了js实现上传图片及时预览的相关资料,具有一定的参考价值,感兴趣的朋友可以参考一下...2016-05-09
  • php实现文件下载实例分享

    举一个案例:复制代码 代码如下:<?phpclass Downfile { function downserver($file_name){$file_path = "./img/".$file_name;//转码,文件名转为gb2312解决中文乱码$file_name = iconv("utf-8","gb2312",$file_name...2014-06-07
  • python opencv通过4坐标剪裁图片

    图片剪裁是常用的方法,那么如何通过4坐标剪裁图片,本文就详细的来介绍一下,感兴趣的小伙伴们可以参考一下...2021-06-04
  • Visual Studio 2015下载和安装图文教程

    这篇文章主要为大家详细介绍了Visual Studio 2015下载和安装图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • 使用PHP下载CSS文件中的图片的代码

    共享一段使用PHP下载CSS文件中的图片的代码 复制代码 代码如下: <?php //note 设置PHP超时时间 set_time_limit(0); //note 取得样式文件内容 $styleFileContent = file_get_contents('images/style.css'); //not...2013-10-04
  • PHP swfupload图片上传的实例代码

    PHP代码如下:复制代码 代码如下:if (isset($_FILES["Filedata"]) || !is_uploaded_file($_FILES["Filedata"]["tmp_name"]) || $_FILES["Filedata"]["error"] != 0) { $upload_file = $_FILES['Filedata']; $fil...2013-10-04
  • 微信小程序如何获取图片宽度与高度

    这篇文章主要给大家介绍了关于微信小程序如何获取图片宽度与高度的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-10
  • C#中图片旋转和翻转(RotateFlipType)用法分析

    这篇文章主要介绍了C#中图片旋转和翻转(RotateFlipType)用法,实例分析了C#图片旋转及翻转Image.RotateFlip方法属性的常用设置技巧,需要的朋友可以参考下...2020-06-25
  • ps怎么制作图片阴影效果

    ps软件是现在很多人比较喜欢的,有着非常不错的使用效果,这次文章就给大家介绍下ps怎么制作图片阴影效果,还不知道制作方法的赶紧来看看。 ps图片阴影效果怎么做方法/...2017-07-06
  • OpenCV如何去除图片中的阴影的实现

    这篇文章主要介绍了OpenCV如何去除图片中的阴影的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-03-29
  • C#将图片和字节流互相转换并显示到页面上

    本文主要介绍用C#实现图片转换成字节流,字节流转换成图片,并根据图片路径返回图片的字节流,有需要的朋友可以参考下...2020-06-25