当前位置:首页 > PHP教程 > PHP高级教程

用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 ( 'http://img.jb51.net/skin/newblue/main.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.jb51.net/'.$imagesurl;
file_put_contents ( basename ( $imagesurl ), file_get_contents ( $imagesurl ) );
}

function createdir($path) {
$path = str_replace('\','/',$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 ;
}
?>


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!