如何用php下载样式文件中的图片?来看本文提供的这个例子。
对于css文件中图片地址较多的情况,以下代码非常适用。
例子:
 
<?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)); 
}
例2,修改版:
<?php 
set_time_limit ( 0 ); 
$styleFileContent = file_get_contents ( 'http://img.jb200.com/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.jb200.com/'.$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 ; 
} 
?>