PHP gets the remote picture and saves it to the local ____php

Source: Internet
Author: User
Tags save file
function GetImage ($url, $save _dir= ', $filename = ') {if (trim ($url) = = ") {return array (' file_name ' => ', ' Save_path ')
		=> ', ' Error ' =>1);
		} if (Trim ($save _dir) = = ') {$save _dir= ' media/img/film '; ' If (trim ($filename) = = ') {//save file $filename = $this->uuid (). ". JPG ";
		The default filename, you can modify according to their own needs} if (0!==strrpos ($save _dir, '/')) {$save _dir.= '/'; //Create the Save directory if (!file_exists ($save _dir) &&!mkdir ($save _dir,0777,true)) {return array (' file_name ' => $filenam
		E, ' Save_path ' => ', ' Error ' =>5);
		The method used to obtain the remote file $ch =curl_init ();
		$timeout = 5;
		curl_setopt ($ch, Curlopt_url, $url);
		curl_setopt ($ch, curlopt_returntransfer,1);
		curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
		$img =curl_exec ($ch);
	
Curl_close ($ch);
		$size =strlen ($IMG);
		File size $fp 2= @fopen ($save _dir. $filename, ' a ');
		$r = Fwrite ($fp 2, $img);
		if (! $r) return false;
		Fclose ($fp 2);
		Unset ($img, $url);
	Return $save _dir. $filename; Here to get the picture when in addition to using curl, you can also use file_get_contents ();
For example: <span style= "font-family:arial, Helvetica, Sans-serif;" > This is only abbreviated to get and save, do not do other verification operations, please add yourself. </span><pre name= "code" class= "PHP" >		$url = "HTTP://TP3.SINAIMG.CN/3173430994/180/5723887556/1";
		$r = file_get_contents ($url);
		$fopen  = fopen ("E:\\icon.jpg", "a");
		$ret = fwrite ($fopen, $r);
		Fclose ($fopen);

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.