phpで実現したcssファイルの中のピクチャをダウンロードするコード

2127 ワード

ベテランでプロフェッショナルなスリとして、中学3年生から偉大なインターネットに投入されてから今まで豊富なスリの経験を蓄積しました.Webを作るプログラマーにも似たような経験があると信じています.
スリの過程で、スタイルファイルの画像をダウンロードする必要があります.膨大なスタイルのファイルに遭遇すると、100以上のダウンロードが必要な画像がある可能性があります.次の小さなコードを使用するのが最適です.
 
  
< ?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));
}

以上は転載の原文で、以下は修正版で、転載するならリンクを残してください.
 
  
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='//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 ;
}
?>