T
T
tincap2015-11-24 07:25:07
PHP
tincap, 2015-11-24 07:25:07

Why doesn't sending a file to cUrl work?

I'm trying to send a file via cUrl, but the transferred data is not perceived as $_FILES, but as plain text.
There is index.php and post.php
index.php

$post = [
    'image' => "@/Library/WebServer/Documents/test/test.jpg",
    'text'  => 'Hello',
];

$ch = curl_init('http://localhost/post.php');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$response = curl_exec($ch);

echo $response;

post.php
echo 'REQUEST' . "\n";
print_r($_REQUEST);

echo 'FILES' . "\n";
print_r($_FILES);

Output:
REQUEST
Array
(
[image] => @/Library/WebServer/Documents/test/test.jpg
[text] => Hello
)
FILES Array ( )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Perevoshchikov, 2015-11-24
@tincap

PHP what version? There was a similar question PHP 5.6 and sending a file via cURL

A
Andrew, 2015-11-24
@R0dger

Try it here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question