Get the remote picture and save it to the local

Source: Internet
Author: User
Tags time and date

<?php

//
Function: Gets the remote picture and saves it to the local
//
//
Determine if you have permission to write files to the local server
//
//
Variable Description:
$url is the full URL address of the remote picture and cannot be empty.
$filename is an optional variable: If NULL, local file names are based on time and date
Automatically generated.

function Grabimage ($url, $filename = "") {
if ($url = = ""): return false;endif;

if ($filename = = "") {
$ext =STRRCHR ($url, ".");
if ($ext!= ". gif" && $ext!= ". jpg"): return false;endif;
$filename =date ("Dmyhis"). $ext;
}

Ob_start ();
ReadFile ($url);
$img = Ob_get_contents ();
Ob_end_clean ();
$size = strlen ($img);

$FP 2= @fopen ($filename, "a");
Fwrite ($fp 2, $img);
Fclose ($fp 2);

return $filename;
}


$img =grabimage ("Http://news.bbc.co.uk/images/_1978837_detector_ap100.jpg", "");
if ($img): Echo ' <pre></pre> ';
Else:echo "false";
endif

?>

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.