PHP get remote picture simulation Post,file upload to specified server

Source: Internet
Author: User

1. Get the remote picture

/**

$Path to save the address of the picture

$url the remote picture address to get

**/

function getimg ($path, $url) {

$aext=Explode‘.‘,$url);
$ext= End ($aext);

$name = $path.' /'. Time () . '. ' . $ext;
$source=file_get_contents ($url);
File_put_contents ($name,$source);
return $name;

}

2. Uploading images

/**

$posturl the address of the uploaded image

$path the address where the local picture is located

**/

function Postimg ($posturl, $path) {

$obj= new curlfile ($path);
$obj-Setmimetype ("Application/octet-stream");The file type must be specified, otherwise default to Application/octet-stream, binary stream file </span>
$post [' Filedata ']=$obj;
$ch=Curl_init ();
curl_setopt ($ch,Curlopt_header,FALSE);
When enabled, a regular post request is sent with the type: application/x-www-form-urlencoded, just as the form was submitted.
curl_setopt ($ch,Curlopt_post,true);
curl_setopt ($ch,Curlopt_returntransfer,true);
curl_setopt ( $ch, curlopt_binarytransfer, true);
curl_setopt ( $ch, Curlopt_postfields, $post);
curl_setopt ( $ch, Curlopt_url, $posturl); //upload class
Span style= "COLOR: #7587a6" > $info = curl_exec ( $ch);
curl_close ( $ch);
return $info;

}

PHP get remote picture simulation Post,file upload to specified server

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.