Home > Backend Development > PHP Tutorial > Code for downloading images in css files using PHP_PHP Tutorial

Code for downloading images in css files using PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:41:27
Original
944 people have browsed it

As a senior and professional scalper, I have accumulated rich experience in scalping since I started to invest in the great Internet in the third grade of junior high school. I believe that every web programmer will have similar experiences.

During the peeling process, it is essential to download the pictures in the style file. When encountering a relatively large style file, in which there may be hundreds of images that need to be downloaded, it is most appropriate to use the following small code.

Copy code The code is as follows:

< ?php
/*
More & Original PHP Framwork
Copyright (c) 2007 - 2008 IsMole Inc.

Author: kimi
Documentation: Download the image in the style file, Shuishui special peeling tool
*/

/ /note Set PHP timeout
set_time_limit(0);

//note Get style file content
$styleFileContent = file_get_contents('images/style.css');

//note Match the URL addresses that need to be downloaded
preg_match_all("/url((.*))/", $styleFileContent, $imagesURLArray);

//note Loop through the addresses that need to be downloaded, one by one Download
$imagesURLArray = array_unique($imagesURLArray[1]);
foreach($imagesURLArray as $imagesURL) {
file_put_contents(basename($imagesURL), file_get_contents($imagesURL));
}

The above is the original text reprinted, and the following is the modified version. Please leave a link if you reprint.

Copy code The code is as follows:

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 directory
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 ;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321144.htmlTechArticleAs a senior and professional scalper, I have accumulated a lot of experience in the great Internet since I was in the third grade of junior high school. Rich peeling experience. I believe every web programmer will also have...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template