O
O
Outoverlay2016-02-25 12:35:23
PHP
Outoverlay, 2016-02-25 12:35:23

How to send a file using curl?

It's strange that uploadpost.php prints the $_POST superglobal: "Array ( [file_box] => @C:/server/images/1.png)"
And the $_FILES superglobal: "array()"
What's the problem?

$post = array(
    "file_box" => "@C:/server/images/1.png"
);

$ch = curl_init();

// сообщаем куда будет отправлять
curl_setopt($ch, CURLOPT_URL, 'http://test.loc/test/uploadpost.php' );

// файлы и данные будет отправлены
curl_setopt($ch, CURLOPT_POSTFIELDS, $post );

// передаем true или 1 если хотим ждать ответа после запроса
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// включим отладочную информацию
curl_setopt($ch, CURLOPT_VERBOSE, true);

$response = curl_exec( $ch );

echo $response;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
IceJOKER, 2016-02-25
@Outoverlay

stackoverflow.com/questions/25934128/curl-file-upl...

E
Eugene, 2016-02-25
@Nc_Soft

if (class_exists('\CURLFile')) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, ['file_box' => new \CURLFile($path)]);
        } else {
            curl_setopt($ch, CURLOPT_POSTFIELDS, ['file_box' => "@$path"]);
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question