PHP <5.5 Curl Post and file request

Source: Internet
Author: User

/**     * cross-domain POST request (adapter file upload)     * @param $url upload address     * @param $post _data request Data     *     /Protected function Sendpost ($ URL, $post _data)    {/        * $url = "http://10.0.0.87/fileAccept.php";        $post _data = Array (            "dir" = "WeChat",            //local file address to be uploaded            "upload" = "@/alidata/www/" 15c56d316166c1fccfb036a9337d0caechrysanthemum.jpg '        ); *        /$ch = Curl_init ();        curl_setopt ($ch, Curlopt_url, $url);        curl_setopt ($ch, Curlopt_returntransfer, 1);        curl_setopt ($ch, Curlopt_post, 1);        curl_setopt ($ch, Curlopt_postfields, $post _data);        $output = curl_exec ($ch);        Curl_close ($ch);        return $output;    }

Receive:

<?php/*** cross-domain File receive */class fileaccept{protected $dis = '/alidata/www/cdnappota/'; protected $ur = ' Http://cdn.multiopen    . cn/';//Function to get the client IP addressfunction get_client_ip () {$ipaddress = ';    if (getenv (' http_client_ip ')) $ipaddress = getenv (' http_client_ip ');    else if (getenv (' http_x_forwarded_for ')) $ipaddress = getenv (' http_x_forwarded_for ');    else if (getenv (' http_x_forwarded ')) $ipaddress = getenv (' http_x_forwarded ');    else if (getenv (' http_forwarded_for ')) $ipaddress = getenv (' http_forwarded_for ');    else if (getenv (' http_forwarded ')) $ipaddress = getenv (' http_forwarded ');    else if (getenv (' remote_addr ')) $ipaddress = getenv (' remote_addr ');    else $ipaddress = ' UNKNOWN '; return $ipaddress;}        function UploadFile ($files, $dir = ' images ') {if ($files) {$time = time ();        $newname = MD5 (Uniqid (). $time);        if (!file_exists ($this->dis $dir. '/')) {mkdir ($this->dis. $dir. ");       }$res = Move_uploaded_file ($files [' Tmp_name '], $this->dis $dir. '/'. $newname. $files [' name ']); if ($res) {echo Json_ Encode (' errno ' =>0, ' data ' =>[$this->ur. $dir. '/'. $newname. $files [' name ']]);} Else{echo Json_encode ([' errno ' =>1]);}}} $files = $_files[' upload '); $dir = $_post[' dir ']; $acc = new Fileaccept (); $ip = $acc->get_client_ip (); $whiteIp = [' 10.0. 0.117 '];if (In_array ($ip, $whiteIp)) {echo $acc->uploadfile ($files, $dir);}

  

PHP <5.5 Curl Post and file request

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.