Using PHP to download images in the CSS file code

  • 2020-03-31 20:21:53
  • OfStack

As a senior and professional pickpocket, I have accumulated rich experience in pickpocketing since I started to invest in the great Internet in junior three. I'm sure every programmer on the web has had a similar experience.

In the process of peeling, it is necessary to download the picture in the style file. With a large style file, where there may be hundreds of images that need to be downloaded, the following little code is most appropriate.
 
< ?php 
 

//Note sets the PHP timeout
set_time_limit(0); 

//Note gets the contents of the style file
$styleFileContent = file_get_contents('images/style.css'); 

//The note matches the URL to be downloaded
preg_match_all("/url((.*))/", $styleFileContent, $imagesURLArray); 

//The note loop requires the address to be downloaded, one by one
$imagesURLArray = array_unique($imagesURLArray[1]); 
foreach($imagesURLArray as $imagesURL) { 
file_put_contents(basename($imagesURL), file_get_contents($imagesURL)); 
} 

The above is reproduced in the original, the following is modified version, reproduced words please leave a link.

 
<?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)) 
{ 
//Create a directory
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 ; 
} 
?> 

Related articles: