Upload a file using file_get_contents

Viewed 41068

I realise I can do this with CURL very easily, but I was wondering if it was possible to use file_get_contents() with the http stream context to upload a file to a remote web server, and if so, how?

2 Answers

Or maybe you can just do :

$postdata = http_build_query(
array(
    'var1' => 'some content',
    'file' => file_get_contents('path/to/file')
)
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-Type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);
$result = file_get_contents('http://example.com/submit.php', false, $context);

You'd change the '/path/to/file' to the appropriate path

Related