m

全干工程师

php使用curl上传文件

<?php

function upload(array $files)
{
    $ch = curl_init();
    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_HEADER => true,
        CURLOPT_NOBODY => false,
        CURLOPT_CUSTOMREQUEST  => 'POST',
        CURLOPT_HTTPHEADER => [
            'Content-Type:multipart/form-data',
            'Authorization:token'
        ],
        CURLOPT_URL => 'https://zhidao.loocode.com/upload',
        CURLOPT_TIMEOUT => 3,
    );
    if (function_exists('curl_file_create')) {
        $post['file'] = curl_file_create($files['tmp_name'], $files['type'], $files['name']);
    } else {
        $post['file'] = '@' . $files['tmp_name'] . ';type=' . $files['type'] . ';filename=' . $files['name'];
    }
    $options[CURLOPT_POSTFIELDS] = $post;
    curl_setopt_array($ch, $options);
    return curl_exec($ch);
}

留言