博客小子:记录我们对互联网和生活的探索! 注册 | 登陆

使用php下载css文件中的图片

Tags: php, css, 图片, 下载

以前还很想自己写一个这样的东西,凑巧在网上看到这么一个东西。

原文地址:http://www.ccvita.com/category/php

 

PHP代码
  1. < ?php   
  2. /*  
  3. More & Original PHP Framwork  
  4. Copyright (c) 2007 - 2008 IsMole Inc.  
  5.  
  6. Author: kimi  
  7. Documentation: 下载样式文件中的图片,水水专用扒皮工具  
  8. */  
  9.   
  10. //note 设置PHP超时时间   
  11. set_time_limit(0);   
  12.   
  13. //note 取得样式文件内容   
  14. $styleFileContent = file_get_contents('images/style.css');   
  15.   
  16. //note 匹配出需要下载的URL地址   
  17. preg_match_all("/url\((.*)\)/"$styleFileContent$imagesURLArray);   
  18.   
  19. //note 循环需要下载的地址,逐个下载   
  20. $imagesURLArray = array_unique($imagesURLArray[1]);   
  21. foreach($imagesURLArray as $imagesURL) {   
  22. file_put_contents(basename($imagesURL), file_get_contents($imagesURL));   
  23. }  

 

大家可劲下载!

以上是转载的原文,下面是blogguy.cn自己的修改版本,转载的话请留个链接。

PHP代码
  1. <?php  
  2. set_time_limit ( 0 );  
  3. $styleFileContent = file_get_contents ( 'http://www.blogguy.cn/stylesheet.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='http://www.blogguy.cn/'.$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. ?>  

Tags: php, css, 图片, 下载

« 上一篇:ubuntu server 下支持gd绘图函数 | 下一篇:apt使用详细说明 »

只显示10条记录相关文章

Trackbacks

点击获得Trackback地址,Encode: UTF-8

2条记录访客评论

这个不错,可以提高“工作效率”

Post by 营养师小陈 on 2009, April 28, 8:51 AM 引用此文发表评论 #1

试试我这个 http://www.sswowo.com/getcssimages/

Post by wowo on 2011, June 8, 3:36 PM 引用此文发表评论 #2


发表评论

评论内容 (必填):