1

Possible Duplicate:
Upload a file using file_get_contents

I want to send a jpg file to another server with file_get_contents.

I use this for POST data but don't works if I put my jpg file in the data array

$postdata = http_build_query(
    array(
        'collection' => 'test',
        'data' =>  array('name'=>'Test name','Surname'=>'Test Surname')
    )
);

$opts = array(
    'http' => array(
        'method' => "POST",
        'header' => "Connection: close\r\n".
                "Content-type: application/x-www-form-urlencoded\r\n".
                "Content-Length: ".strlen($postdata)."\r\n",
        'content' => $postdata
    )
);

$context = stream_context_create($opts);

$result = file_get_contents('http://localhost/test/index.php/save-doc', false, $context);
Ahmad Adibzad
  • 501
  • 2
  • 6
  • 14
Beka Tomashvili
  • 2,171
  • 5
  • 21
  • 27
  • 5
    Consider using CURL for this task. – Aleks G Dec 19 '12 at 15:14
  • I dont understand why you are using file_get_contents when you want to post something. – Bhavik Shah Dec 19 '12 at 15:16
  • `file_get_contents` is meant to read a file, not to send it. To send something, use a different method, like [`cURL`](http://php.net/manual/en/book.curl.php) as [AleksG](http://stackoverflow.com/users/717214/aleks-g) suggested. – Peon Dec 19 '12 at 15:16
  • 5
    You have to use a multi-part `Content-type` header. See http://stackoverflow.com/questions/4003989/upload-a-file-using-file-get-contents – Sherif Dec 19 '12 at 15:16
  • 2
    There seems to be a bit of confusion here. Yes `file_get_contents` is meant to read files, but it can also read **responses from page requests**. Using the stream context it _should_ indeed be possible to **send** data using it. – Leigh Dec 19 '12 at 15:49

0 Answers0