php下载css文件中图片的实现代码

WBOY
发布: 2016-07-25 08:52:27
原创
932 人浏览过
  1. /*

  2. More & Original PHP Framwork
  3. Copyright (c) 2007 - 2008 IsMole Inc.
  4. Author: kimi

  5. Documentation: 下载样式文件中的图片,水水专用扒皮工具
  6. */
  7. //note 设置PHP超时时间

  8. set_time_limit(0);
  9. //note 取得样式文件内容

  10. $styleFileContent = file_get_contents('images/style.css');
  11. //note 匹配出需要下载的URL地址

  12. preg_match_all("/url\((.*)\)/", $styleFileContent, $imagesURLArray);
  13. //note 循环需要下载的地址,逐个下载

  14. $imagesURLArray = array_unique($imagesURLArray[1]);
  15. foreach($imagesURLArray as $imagesURL) {
  16. file_put_contents(basename($imagesURL), file_get_contents($imagesURL));
  17. }
  18. 例2,修改版:

  19. set_time_limit ( 0 );
  20. $styleFileContent = file_get_contents ( 'http://img.jbxue.com/skin/newblue/main.css' );
  21. preg_match_all ( "/url\((.*)\)/", $styleFileContent, $imagesURLArray );
  22. $imagesURLArray = array_unique ( $imagesURLArray [1] );
  23. foreach ( $imagesURLArray as $imagesURL ) {
  24. $dir=dirname($imagesURL);
  25. if(!file_exists($dir))
  26. {
  27. //创建目录
  28. createDir($dir);
  29. }
  30. $imagesURL='http://bbs.it-home.org/'.$imagesURL;
  31. file_put_contents ( basename ( $imagesURL ), file_get_contents ( $imagesURL ) );
  32. }
  33. function createDir($path) {

  34. $path = str_replace('\\','/',$path) ;
  35. if ( is_dir($path) ) return true ;
  36. if ( file_exists($path) ) return false ;
  37. $parent = substr($path ,0, strrpos($path,'/') ) ;

  38. if ( $parent==='' || $parent==='.' || createDir( $parent ) )
  39. return @mkdir($path) ;
  40. else return false ;
  41. }
  42. ?>
复制代码


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!