服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > php教程 >

PHP下载CSS文件中的图片,扒站的代码

时间:2011-07-29 18:50来源:未知 作者:最模板 点击:
PHP下载CSS文件中的图片,扒站的代码

有时候仿造模板, 必不可少的需要下载样式文件中的图片。碰到比较庞大的样式文件,其中可能会有上百个需要下载的图片,那么使用下面这段小代码是最为合适的了。

一、转载的版本

< ?php
/*
More & Original PHP Framwork
Copyright (c) 2007 - 2008 IsMole Inc.

Author: 最模板
Documentation: 下载样式文件中的图片,www.zuimoban.com
*/

//note 设置PHP超时时间
set_time_limit(0);

//note 取得样式文件内容
$styleFileContent = file_get_contents('images/style.css');

//note 匹配出需要下载的URL地址
preg_match_all("/url\((.*)\)/", $styleFileContent, $imagesURLArray);

//note 循环需要下载的地址,逐个下载
$imagesURLArray = array_unique($imagesURLArray[1]);
foreach($imagesURLArray as $imagesURL) {
file_put_contents(basename($imagesURL), file_get_contents($imagesURL));
}

 

二、下面是修改版本,转载的话请留个链接 

  1. <?php  
  2. set_time_limit ( 0 );  
  3. $styleFileContent = file_get_contents ( '//www.zuimoban.com/css/css.css' );  
  4. preg_match_all ( "/url\((.*)\)/"$styleFileContent$imagesURLArray );  
  5. $imagesURLArray = array_unique ( $imagesURLArray [1] );  
  6. foreach ( $imagesURLArray as $imagesURL ) {  
  7. $dir=dirname($imagesURL);  
  8. if(!file_exists($dir))  
  9. {  
  10. //??  
  11. createDir($dir);  
  12. }  
  13. $imagesURL='//www.zuimoban.com/'.$imagesURL;  
  14. file_put_contents ( basename ( $imagesURL ), file_get_contents ( $imagesURL ) );  
  15. }  
  16.  
  17. function createDir($path) {  
  18. $path = str_replace('\\','/',$path) ;  
  19. if ( is_dir($path) ) return true ;  
  20. if ( file_exists($path) ) return false ;  
  21.  
  22. $parent = substr($path ,0, strrpos($path,'/') ) ;  
  23. if ( $parent==='' || $parent==='.' || createDir( $parent ) )  
  24. return @mkdir($path) ;  
  25. else return false ;  
  26. }  
  27. ?> 

 

(责任编辑:最模板)
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容