PHP Save Remote Pictures

Source: Internet
Author: User
Tags filetime php code

PHP Save the remote picture , I This system is written by myself, so upload the picture when quite paralyzed, I also used a new cloud of a system, he that editor support remote save picture function, But that's ASP and my system only supports PHP I think PHP may be easier than the ASP to implement this function, began to think with file_get_contents (), and then use fopen to save file_get_content get content on the OK, Here is a small program that I have implemented.

<?php
$filed = "Yun_qi_img/conference_php_quebec.gif";
$datad =file_get_contents ($filed);
$mydr =basename ($file);
Fwrite (fopen ($mydr, "wb+"), $data);
?>

It's easy, but I don't think it's going to happen. So I wrote the following code

PHP Code:
if ($_post[' savepic ']== "checked") {

$img _array = Array ();
$content 1 = stripslashes ($_post[' post_content '));
if (GET_MAGIC_QUOTES_GPC ()) $content 1 = stripslashes ($content 1);

Preg_match_all ("/(src| SRC) =\ "(http:\/\/(. +). ( gif|jpg|jpeg|bmp|png))/isu ", $content 1, $img _array);//start matching all pictures and put data
$img _array = Array_unique (Dhtmlspecialchars ($img _array[2]));

Print_r ($img _array);
Set_time_limit (0);
foreach ($img _array as $key => $value) {
$get _file = file_get_contents ($value);//start getting pictures now OH
$filetime = time ();
$filepath = ". /wp-content/uploads/pic2/". Date (" Y ", $filetime)." /". Date (" M ", $filetime)." /";//Picture Saved path directory
!is_dir ($filepath)? Mkdirs ($filepath): null;
$filename = Date ("Ymdhis", $filetime). Random (1). substr ($value, -3,3);
$fp = @fopen ($filepath. $filename, "w");
@fwrite ($fp, $get _file);
Fclose ($FP)//finished, ha
$content 1 = preg_replace ("/". Addcslashes ($value, "/"). " /isu ","/wp-content/uploads/pic2/". Date (" Y ", $filetime)." /". Date (" M ", $filetime)."  /". $filename, $content 1); By the way, replace the picture address inside the article

}
The above function realizes that all the pictures in your editor are saved.

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.