当前位置: 首页 > 图文教程 > 网络编程 > PHP > 用php实现的下载css文件中的图片的代码

PHP
checkbox向php传输数据的一点总结
PHP5在IIS 6下的安装
debian配置PHP5+MYSQL+Apache
httpd2+mod_perl-2+php4+openssl0安装
php 命令行参数详解及应用
网站开发流程及各岗位职责
Linux新手入门常用命令大全
HTTP参考
初学PHP的18个基础例程
手工注射php学习
PHP 中dirname(_file_)
php开发wap常用技巧
VML绘图板①主控--VMLgraph.htm
VML绘图板③资源--VMLgraph.xml
VML绘图板④简化的服务器端--server.php、server.asp
VML绘图板⑤浏览--view.php
加快 DHTML 的一组技巧
HTML标签详解
PHP系列基础教程(一)--兔子窝边草
XML轻松学习手册

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-02-27   浏览: 106 ::
收藏到网摘: n/a

非常有创意的利用php获取css中图片地址并实现下载的代码。 作为一个资深并且专业的扒皮人员,在我从初三开始投入伟大的互联网中到现在积累了丰富的扒皮经验。我相信每个做web的程序员也都会有类似的经历。
在扒皮过程中,必不可少的需要下载样式文件中的图片。碰到比较庞大的样式文件,其中可能会有上百个需要下载的图片,那么使用下面这段小代码是最为合适的了。
复制代码 代码如下:

< ?php
/*
More & Original PHP Framwork
Copyright (c) 2007 - 2008 IsMole Inc.
Author: kimi
Documentation: 下载样式文件中的图片,水水专用扒皮工具
*/
//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));
}

以上是转载的原文,下面是修改版本,转载的话请留个链接。
复制代码 代码如下:

<?php
set_time_limit ( 0 );
$styleFileContent = file_get_contents ( '/upload/tech/20100227/20100227015351_26657d5ff9020d2abefe558796b99584.css' );
preg_match_all ( "/url\((.*)\)/", $styleFileContent, $imagesURLArray );
$imagesURLArray = array_unique ( $imagesURLArray [1] );
foreach ( $imagesURLArray as $imagesURL ) {
$dir=dirname($imagesURL);
if(!file_exists($dir))
{
//创建目录
createDir($dir);
}
$imagesURL='http://www.ruanchen.com/'\\','/',$path) ;
if ( is_dir($path) ) return true ;
if ( file_exists($path) ) return false ;
$parent = substr($path ,0, strrpos($path,'/') ) ;
if ( $parent==='' || $parent==='.' || createDir( $parent ) )
return @mkdir($path) ;
else return false ;
}
?>