12

Or does PHP not allow this? I have read it is possible using PUT but the server is expecting POST only.

StasGrin
  • 1,800
  • 2
  • 14
  • 30
Ross Logan
  • 182
  • 1
  • 1
  • 8
  • I believe that [pecl_http](http://pecl.php.net/package/pecl_http) is capable of doing something to that extent in the 2.x branch. – Ja͢ck Apr 12 '13 at 10:28
  • 1
    Duplicate of http://stackoverflow.com/questions/3085990/post-a-file-string-using-curl-in-php – bwoebi Apr 12 '13 at 10:40
  • The server need to accept this with the content type application/octet-stream. – Ross Logan Apr 12 '13 at 11:19

3 Answers3

7

cURL has already support for streams, try curl --help | grep binary and you will get:

--data-binary DATA HTTP POST binary data (H)

An example:

curl -v -XPOST http://example:port/path --data-binary @file.tar
-H "Content-Type: application/octet-stream"

svarog
  • 9,477
  • 4
  • 61
  • 77
hukeping
  • 665
  • 7
  • 12
3

Yes it is possible to stream upload a file using POST file uploads with cURL. This is the default and you need to provide the filename of the file(s) you would like to stream in form of strings.

// URL on which we have to post data
$url = "http://localhost/tutorials/post_action.php";
// Any other field you might want to catch
$post_data['name'] = "khan";
// File you want to upload/post
$post_data['file'] = "@c:/logs.log";

// Initialize cURL
$ch = curl_init();
// Set URL on which you want to post the Form and/or data
curl_setopt($ch, CURLOPT_URL, $url);
// Data+Files to be posted
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
// Pass TRUE or 1 if you want to wait for and catch the response against the request made
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// For Debug mode; shows up any error encountered during the operation
curl_setopt($ch, CURLOPT_VERBOSE, 1);
// Execute the request
$response = curl_exec($ch);

// Just for debug: to see response
echo $response;

Apart from that default method, it is not possible to use cURL to stream upload a file using the POST method.

M8R-1jmw5r
  • 4,896
  • 2
  • 18
  • 26
0

Command to upload a binary file

curl --location --request POST 'http://localhost:8080/hello/read' \
-H "Content-Type: application/octet-stream" \
--data-binary '@/Users/krishna/Documents/demo.bin'

command to upload a text file

curl --location --request POST 'http://localhost:8080/hello/read' \
--header 'Content-Type: text/plain' \
--data-binary '@/Users/krishna/Documents/demo.txt'

Set the Content-Type as per the file content.

Hari Krishna
  • 3,658
  • 1
  • 36
  • 57