Php batch download web page images and replace the path with local_php tutorial

Source: Internet
Author: User
Php downloads Web images in batches and replaces them with the local path. Now we want to copy the content with images from other websites to our website. in this way, I have to download images from other websites and save them to my local computer, replace the image address in the content with the image address. now we want to copy the image content from another website to our website. in this way, I must download the image from another website and save it locally, replace the image address in the content with our local address. here we need to use the three functions preg_match_all, file_get_contents, and str_replace in php.


I copied an article and found that the image paths are from other websites. how can I download these images to a local directory with one click and change them to a local path.

The code is as follows:
/**
* Obtain the image path in the replacement document.
* @ Param string $ xstr content of the webpage
* @ Param string $ the name of the file in which the keyword creates the photo. I wrote upimg.
* @ Param string $ oriweb URL is generally null
* @ Return string
*
*/
Function replaceimg ($ xstr, $ keyword, $ oriweb ){
$ Basedir = dirname (_ FILE __);

// Save path
$ D = date ('ymm', time ());
$ Dirslsitss = $ basedir. '/.../uploads/'. $ keyword. '/'. $ d; // whether the category exists
If (! Is_dir ($ dirslsitss )){
@ Mkdir ($ dirserver load balancer, 0777 );
}

// Match the image's src
Preg_match_all ('#] *> # I', $ xstr, $ match );

Foreach ($ match [1] as $ imgurl ){

$ Imgurl = $ imgurl;

If (is_int (strpos ($ imgurl, 'http '))){
$ Arcurl = $ imgurl;
} Else {
$ Arcurl = $ oriweb. $ imgurl;
}
$ Img = file_get_contents ($ arcurl );


If (! Empty ($ img )){

// Save the image to the server
$ Fileimgname = time (). "-". rand (, 9999). ". jpg ";
$ Filecachs = $ dirslsitss. "/". $ fileimgname;
$ Fanhuistr = file_put_contents ($ filecachs, $ img );
$ Saveimgfile = "/uploads/$ keyword". "/". $ d. "/". $ fileimgname;


$ Xstr = str_replace ($ imgurl, $ saveimgfile, $ xstr );
}
}
Return $ xstr;
}

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.