Simulate PHP curl uploading files to a remote server

Source: Internet
Author: User

test.php:

<?php
Header (' Content-type:text/html;charset=utf8 ');

$ch = Curl_init ();

Add @ Sign Curl will take it as a file upload process
$data = Array (' img ' = ' @ '. DirName (__file__). ' /img/1.jpg ');
curl_setopt ($ch, Curlopt_url, "http://localhost:8088/curl/get_img.php");
curl_setopt ($ch, curlopt_returntransfer,true);
curl_setopt ($ch, curlopt_post,true);
curl_setopt ($ch, Curlopt_postfields, $data);
$result = curl_exec ($ch);
Curl_close ($ch);
echo Json_decode ($result);

?>

get_img.php:

<?php
if ($_files) {
$filename = $_files[' img ' [' name '];
$tmpname = $_files[' img ' [' tmp_name '];
if (Move_uploaded_file ($tmpname, DirName (__file__). ' /upload/'. $filename)) {
echo Json_encode (' upload successful ');
}else{
$data = Json_encode ($_files);
Echo $data;
}
}

?>

When I visit test.php, "Upload succeeded" will be displayed, and a 1.jpg picture file will be generated under the Upload folder. Simulate PHP curl uploading files to a remote server

Simulate PHP curl uploading files to a remote 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.