search
HomeBackend DevelopmentPHP TutorialFunction code for downloading remote images in content through regular expressions in PHP_PHP Tutorial

Function code for downloading remote images in content through regular expressions in PHP_PHP Tutorial

Jul 21, 2016 pm 03:22 PM
phpdownloadcodecontentfunctionpicturebelongregularofprogramexpressionRemotelypass

This program is actually an important part of the "thief program". This section of the program is just the section for downloading remote images. The program is relatively simple to write, and most parts are commented.

Copy code The code is as follows:

if (preg_match_all("/http://[^ "']+[. jpg|.gif|.jpeg|.png]+/ui",stripcslashes($content),$aliurl)){
$i=0; //Multiple files++
while(list($ key ,$v) = each($aliurl[0])){
//echo $v."
";
$filetype = pathinfo($v, PATHINFO_EXTENSION); // Get the suffix name
$ff = @file_get_contents($v); //Get the binary file content
if(!stripos($v,"jb51.net")){//Determine whether it is your own website The picture below
if (!empty($ff)){ //Perform the following operations after obtaining the file
$dir = "upload/".date("Ymd")."/";// Specify a new storage path
if (!file_exists($dir)){//Determine whether the directory exists
@mkdir($dir,511,true); //Create a multi-level directory, 511 is converted into decimal 777 has executable permissions
}
$nfn = $dir.date("Ymdhis").$i.".".$filetype; //New name of build file
$nf = @fopen ($nfn,"w"); //Create file
fwrite($nf,$ff); //Write file
fclose($nf); //Close file
$i++; / /Multiple files++
echo "Function code for downloading remote images in content through regular expressions in PHP_PHP Tutorial";
$content = str_replace($v,$nfn, $content);//Replace content Parameters
}else{//If the image cannot be obtained, replace it with the default image
$content = str_replace($v,"/upload/201204/20120417213810742.gif", $content);//Replace content Parameters
}
}
}
}

PHP implementation code for downloading images to local through regular expressions
Copy code The code is as follows:

/*
author: ssh_kobe
date: 20110602
shortage: If in the web page If the image path is not an absolute path, it cannot be crawled
*/
set_time_limit(0);//The crawling is not limited by time

$URL='http://pp.baidu. com/';//Any URL

get_pic($URL);

function get_pic($pic_url) {
//Get the image binary stream
$data=CurlGet( $pic_url);
/*Use regular expressions to get image links*/
$pattern_src = '//';
$num = preg_match_all($pattern_src, $data, $match_src);
$arr_src= $match_src[1];//Get the image array
get_name($arr_src);

echo "
finished!!!";
return 0;
}

/*Get the picture type and save it to the same directory as the file*/
function get_name($pic_arr)
{
//Picture type
$pattern_type = '/ (/.(jpg|bmp|jpeg|gif|png))/';

foreach($pic_arr as $pic_item){//Loop out the address of each picture
$num = preg_match_all( $pattern_type, $pic_item, $match_type);
$pic_name = get_unique().$match_type[1][0];//Change the microsecond timestamp naming
//Save the picture in the form of a stream
$write_fd = @fopen($pic_name,"wb");
@fwrite($write_fd, CurlGet($pic_item));
@fclose($write_fd);
echo "[OK] ..!";
}
return 0;
}

//Get the unique ID through microsecond time
function get_unique(){
list($msec, $sec) = explode(" ",microtime());
return $sec.intval($msec*1000000);
}

//Capture web page content
function CurlGet ($url){
$url=str_replace('&','&',$url);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);

//curl_setopt($curl, CURLOPT_REFERER,$url);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0 ; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2)");
curl_setopt($curl, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt' );
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
$values ​​= curl_exec($curl);
curl_close($curl);
return $values;
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324775.htmlTechArticleThis program is actually an important part of the "thief program". This section of the program is just the section for downloading remote images. The program is relatively simple to write, and most parts are annotated. ...
Statement
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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!