Hướng dẫn php curl upload multiple files - php curl tải lên nhiều tệp

Php 5.5+ có một chức năng để tạo các tệp mà không cần sử dụng curlfile: curl_file_create.

Bạn có thể tải lên nhiều tệp như thế này:

 '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);

Điều này sẽ được nhận trên máy chủ dưới dạng một mảng trong file giống như khi có biểu mẫu được đăng, đây là $_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
                )

        )

)

Đây là $_POST:

Array
(
    [somevar] => hello
)

Ở đây tôi chia sẻ một đoạn mã sẽ hiển thị cách chúng ta có thể sử dụng PHP Curl để tải lên tệp lên máy chủ từ xa dưới dạng yêu cầu bài đăng HTTP.Chúng tôi có thể tải lên nhiều tệp bằng mã này.Nó xử lý các phiên bản PHP đến trước 5.6 cũng như phiên bản PHP 5.5 trở lên.



// URL to which files to be uploaded.
$url = "http://example.com/upload";

// Puts files to be uploaded in this array
$files = array(
  '/path/to/file/one.png',
  '/path/to/file/two.jpg',
);

$postfields = array();

foreach ($files as $index => $file) {
  if (function_exists('curl_file_create')) { // For PHP 5.5+
    $file = curl_file_create($file);
  } else {
    $file = '@' . realpath($file);
  }
  $postfields["file_$index"] = $file;
}

// Add other post data as well.
$postfields['name'] = 'Name';
// Need to set this head for uploading file.
$headers = array("Content-Type" => "multipart/form-data");

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

$response = curl_exec($ch);

if(!curl_errno($ch))
{
    $info = curl_getinfo($ch);
    if ($info['http_code'] == 200) {
      // Files uploaded successfully.
    }
}
else
{
  // Error happened
  $error_message = curl_error($ch);
}
curl_close($ch);

Làm thế nào để tải lên nhiều tệp bằng Curl trong PHP?

Hiển thị hoạt động trên bài viết này..Tệp [x] '=> Đối tượng Curlfile ...]]]'mode' => 'combine', 'input' => 'upload', 'format' => $outputformat, 'files[0]' => CURLFile object, 'files[1]' => CURLFile object, 'files[x]' => CURLFile object ... ] ]

Làm cách nào để gửi nhiều tệp bằng lệnh Curl?

Để đăng một tệp với Curl, sử dụng các tùy chọn dòng lệnh -D hoặc -F và khởi động dữ liệu bằng ký hiệu @ theo sau là tên tệp.Để gửi nhiều tệp, hãy lặp lại tùy chọn -f nhiều lần.repeat the -F option several times.