当前位置:Gxlcms > PHP教程 > php下载css文件中图片的实现代码

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

时间:2021-07-01 10:21:17 帮助过:22人阅读

  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. ?>

人气教程排行