关于curl模拟上传文件

在用curl模拟上传的时候遇到一个小问题,所以记录下来:

  • 在 curl 中设置表单,包括文件上传网上很多都是这样的
 $post_data = array(
        "file" => "@" . "文件所在路径"
    );

 $ch = curl_init();
 curl_setopt($ch, CURLOPT_HTTPHEADER, explode("\r\n", $header));//模拟的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);
 curl_close($ch); 

但是我发现根本就接受不到文件 $_FILES 这个参数数组:
后来百度查看了一下发现原来 php5.5 之前可以用这样的用法,PHP5.5 之后就用一个

new CURLFile(realpath($file_path), $mime_type, $file_name); 

来进行传参数了:

$post_data['file'] = new CURLFile(realpath($file_path), $mime_type, $file_name);

文件的 mime 类型可以根据下面的函数求得:

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'   => 'application/vnd.ms-powerpoint',
            'pdf'   => 'application/pdf',
            'xml'   => 'application/xml',
            'odt'   => 'application/vnd.oasis.opendocument.text',
            'swf'   => 'application/x-shockwave-flash',
            // archives
            'gz'    => 'application/x-gzip',
            'tgz'   => 'application/x-gzip',
            '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',
            // texts
            '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'  => 'text/x-java-source',
            'rb'    => 'text/x-ruby',
            'sh'    => 'text/x-shellscript',
            'pl'    => 'text/x-perl',
            'sql'   => 'text/x-sql',
            // images
            'bmp'   => 'image/x-ms-bmp',
            'jpg'   => 'image/jpeg',
            'jpeg'  => 'image/jpeg',
            'gif'   => 'image/gif',
            'png'   => 'image/png',
            'tif'   => 'image/tiff',
            'tiff'  => 'image/tiff',
            'tga'   => 'image/x-targa',
            'psd'   => 'image/vnd.adobe.photoshop',
            //audio
            'mp3'   => 'audio/mpeg',
            'mid'   => 'audio/midi',
            'ogg'   => 'audio/ogg',
            'mp4a'  => 'audio/mp4',
            'wav'   => 'audio/wav',
            'wma'   => 'audio/x-ms-wma',
            // video
            'avi'   => 'video/x-msvideo',
            'dv'    => 'video/x-dv',
            'mp4'   => 'video/mp4',
            'mpeg'  => 'video/mpeg',
            'mpg'   => 'video/mpeg',
            'mov'   => 'video/quicktime',
            'wm'    => 'video/x-ms-wmv',
            'flv'   => 'video/x-flv',
            'mkv'   => 'video/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];
    }

具体的可以参考
不同版本PHP之间cURL的区别

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值