使用curl上传多个文件

时间:2017-06-10 14:02:03

标签: php file curl upload

如何使用CURLFilecurl_setopt

在阵列中上传多个文件

使用数据数组会导致错误(无法将数组转换为字符串)和http_build_query数据会破坏CURLFile个对象。

我必须上传的数据如下:

[
    'mode' => 'combine',
    'input' => 'upload',
    'format' => $outputformat,
    'files' => [
        [0] => CURLFile object,
        [1] => CURLFile object,
        [x] => ...
    ]
]

1 个答案:

答案 0 :(得分:2)

PHP 5.5+具有在不使用CURLFile的情况下创建文件的功能:curl_file_create

您可以上传多个文件:

import { takeEvery, all } from 'redux-saga/effects';

...

function *watchAll() {
  yield all([
    takeEvery("FRIEND_FETCH_REQUESTED", fetchFriends),
    takeEvery("CREATE_USER_REQUESTED", createUser)
  ]);
}

这将作为<?php $files = [ '/var/file/something.txt', '/home/another_file.txt', ]; // Set postdata array $postData = ['somevar' => 'hello']; // Create array of files to post foreach ($files as $index => $file) { $postData['file[' . $index . ']'] = curl_file_create( realpath($file), mime_content_type($file), basename($file) ); } $request = curl_init('https://localhost/upload'); curl_setopt($request, CURLOPT_POST, true); curl_setopt($request, CURLOPT_POSTFIELDS, $postData); curl_setopt($request, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($request); if ($result === false) { error_log(curl_error($request)); } curl_close($request); 下的数组在服务器上收到,就像发布的表单一样,这是file

$_FILES

这是Array ( [file] => Array ( [name] => Array ( [0] => 1.txt [1] => 2.txt ) [type] => Array ( [0] => text/plain [1] => text/plain ) [tmp_name] => Array ( [0] => /private/var/folders/cq/7262ntt15mqdmylblg9p1wf80000gn/T/phpp8SsKD [1] => /private/var/folders/cq/7262ntt15mqdmylblg9p1wf80000gn/T/php73ijEP ) [error] => Array ( [0] => 0 [1] => 0 ) [size] => Array ( [0] => 6 [1] => 6 ) ) )

$_POST