4

I am going to convert some file using php and send it as a part of HTTP POST request. There is part of my code:

        $context = stream_context_create(array(
        'http' => array(
            'method' => 'POST',
            'header' => "Content-type: " . $this->contentType."",
            'content' => "file=".$file
        )
            ));
    $data = file_get_contents($this->url, false, $context);

Does variable $file have to be byte representation of the file which I want to send?

And is that correct way to send file in php without using form? Have you got any clues?

Also what is the way to convert file to byte representation using PHP?

3
  • Can you be more specific what do you mean in converting to binary? and what kind of files do you have? Commented Jun 27, 2012 at 14:05
  • In current case I want to send xml file, but I think the code above can be use to send any type of file - and I want that. Converting to binary - converting file to byte array. Commented Jun 27, 2012 at 14:12
  • 1
    You can directly send xml files. CURL is great for this. Commented Jun 27, 2012 at 14:14

2 Answers 2

2

You may find it much easier to use CURL, for example:

function curlPost($url,$file) {
  $ch = curl_init();
  if (!is_resource($ch)) return false;
  curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER , 0 );
  curl_setopt( $ch , CURLOPT_FOLLOWLOCATION , 0 );
  curl_setopt( $ch , CURLOPT_URL , $url );
  curl_setopt( $ch , CURLOPT_POST , 1 );
  curl_setopt( $ch , CURLOPT_POSTFIELDS , '@' . $file );
  curl_setopt( $ch , CURLOPT_RETURNTRANSFER , 1 );
  curl_setopt( $ch , CURLOPT_VERBOSE , 0 );
  $response = curl_exec($ch);
  curl_close($ch);
  return $response;
}

Where $url is where you want to post to, and $file is the path to the file you want to send.

Sign up to request clarification or add additional context in comments.

1 Comment

OK, this is true. But what if my restriction is that php server which I want to do that doesn't have installed curl extension?
1

Oddly enough I just wrote an article and illustrated this same scenario. (phpmaster.com/5-inspiring-and-useful-php-snippets). But to get you started, here's code that should work:

<?php
$context = stream_context_create(array(
        "http" => array(
            "method" => "POST",
            "header" => "Content-Type: multipart/form-data; boundary=--foo\r\n",
            "content" => "--foo\r\n"
                . "Content-Disposition: form-data; name=\"myFile\"; filename=\"image.jpg\"\r\n"
                . "Content-Type: image/jpeg\r\n\r\n"
                . file_get_contents("image.jpg") . "\r\n"
                . "--foo--"
        )
    ));

    $html = file_get_contents("http://example.com/upload.php", false, $context);

In situations like these it helps to make a mock web form and run it through Firefox with firebug enabled or something, and then inspect the request that was sent. From there you can deduce the important things to include.

1 Comment

Finally I used curl to send file with data. It is very easy to install and use. Thank you all for clues. There is code which I used: blog.derakkilgo.com/2009/06/07/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.