About Curl Analog upload files

Source: Internet
Author: User
Tags curl rar

There is a small problem when using curl to simulate uploads, so write it down: set up forms in curl, including files uploaded online a lot of it.

$post _data = Array (
        "file" => "@". "Path where the file is located"
    );

 $ch = Curl_init ();
 curl_setopt ($ch, Curlopt_httpheader, explode ("\ r \ n", $header))//Analog header 
 curl_setopt ($ch, Curlopt_ Returntransfer, true);
 curl_setopt ($ch, Curlopt_url, $url); 
 curl_setopt ($ch, Curlopt_post, true); 
 curl_setopt ($ch, Curlopt_timeout, $time _out); 
 curl_setopt ($ch, Curlopt_cookie, $cookie);
 Post Data
 curl_setopt ($ch, Curlopt_postfields, $post _data);

 $result = curl_exec ($ch);
 $info = Curl_getinfo ($ch);
 

But I found out that I couldn't accept the file $_files this parameter array:
Then Baidu looked at the original php5.5 can be used before the use of this, PHP5.5 after the use of a

To pass the parameters:

$post _data[' file '] = new Curlfile (Realpath ($file _path), $mime _type, $file _name);

The MIME type of a file can be evaluated based on the following function:

function _getmimedetect () {if (class_exists (' Finfo ')) {return ' finfo ';
        else if (function_exists (' Mime_content_type ')) {return ' Mime_content_type ';
            else if (function_exists (' exec ')) {$result = exec (' File-ib '. Escapeshellarg (__file__));
            if (0 = = Strpos ($result, ' text/x-php ') | | 0 = = Strpos ($result, ' text/x-c++ ')) {return ' Linux ';
            $result = EXEC (' File-ib '. Escapeshellarg (__file__));
            if (0 = = Strpos ($result, ' text/x-php ') | | 0 = = Strpos ($result, ' text/x-c++ ')) {return ' BSD ';
    } return ' internal '; function _getmimetype ($path) {$mime = array (//applications ' ai ' => ' application
            /postscript ', ' eps ' => ' application/postscript ', ' exe ' => ' Application/octet-stream ', ' Doc ' => ' Application/vnd.ms-word ', 'XLS ' => ' application/vnd.ms-excel ', ' ppt ' => ' application/vnd.ms-powerpoint ', ' pps ' =& Gt 
            ' Application/vnd.ms-powerpoint ', ' pdf ' => ' application/pdf ', ' xml ' => ' Application/xml ', ' Odt ' => ' application/vnd.oasis.opendocument.text ', ' swf ' => ' application/x-shockwave-f Lash ',//Archives ' GZ ' => ' application/x-gzip ', ' tgz ' => '   IP ', ' BZ ' => ' application/x-bzip2 ', ' bz2 ' => ' application/x-bzip2 ', ' tbz '
            => ' application/x-bzip2 ', ' Zip ' => ' application/zip ', ' rar ' => ' Application/x-rar ', ' Tar ' => ' application/x-tar ', ' 7z ' => ' application/x-7z-compressed ',//Tex
            TS ' txt ' => ' text/plain ', ' php ' => ' text/x-php ', ' html ' => ' text/html ',   ' HTM '=> ' text/html ', ' js ' => ' text/javascript ', ' css ' => ' text/css ', ' RTF ' => ' text/rtf ', ' rtfd ' => ' text/rtfd ', ' py ' => ' Text/x-python ', ' Java ' =& Gt
            ' Text/x-java-source ', ' RB ' => ' text/x-ruby ', ' sh ' => ' text/x-shellscript ', ' PL ' => ' text/x-perl ', ' sql ' => ' text/x-sql ',//images ' BMP ' => ' ima Ge/x-ms-bmp ', ' jpg ' => ' image/jpeg ', ' jpeg ' => ' image/jpeg ', ' gif ' => ' I Mage/gif ', ' png ' => ' image/png ', ' tif ' => ' Image/tiff ', ' TIFF ' => ' image /tiff ', ' TGA ' => ' image/x-targa ', ' psd ' => ' image/vnd.adobe.photoshop ',//au 
            Dio ' mp3 ' => ' audio/mpeg ', ' mid ' => ' Audio/midi ', ' ogg ' => ' Audio/ogg ', ' MP4A ' => ' Audio/mp4 ', ' wav ' => ' audio/wav ', ' wma ' => ' audio/x-ms-wma ',//vide O ' avi ' => ' video/x-msvideo ', ' dv ' => ' VIDEO/X-DV ', ' mp4 ' => ' 4 ', ' mpeg ' => ' video/mpeg ', ' mpg ' => ' video/mpeg ', ' mov ' => ' Video/quick Time ', ' wm ' => ' video/x-ms-wmv ', ' flv ' => ' video/x-flv ', ' mkv ' => ' vid

        Eo/x-matroska ',);
        $fmime = _getmimedetect ();
            Switch ($fmime) {case ' finfo ': $finfo = Finfo_open (fileinfo_mime);
            if ($finfo) $type = @finfo_file ($finfo, $path);
            Break
            Case ' Mime_content_type ': $type = Mime_content_type ($path);
            Break
            Case ' Linux ': $type = exec (' File-ib '. Escapeshellarg ($path));
            Break Case ' BSD ': $type = EXec (' File-ib '. Escapeshellarg ($path));
            Break
            Default: $pinfo = PathInfo ($path); $ext = Isset ($pinfo [' extension '])?
            Strtolower ($pinfo [' extension ']): '; $type = Isset ($mime [$ext])?
            $mime [$ext]: ' Unkown ';
        Break

        $type = explode ('; ', $type); 
            if ($fmime!= ' internal ' && $type [0] = = ' Application/octet-stream ') {$pinfo = PathInfo ($path); $ext = Isset ($pinfo [' extension '])?
            Strtolower ($pinfo [' extension ']): ';
            if (!empty ($ext) &&!empty ($mime [$ext])) {$type [0] = $mime [$ext];
    } return $type [0]; }

Specific can refer to
different versions of PHP Curl difference

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.