<?php
# The boundary can be any string that is not contained in the content body. I will usually use a timestamp
# http://stackoverflow.com/questions/4003989/upload-a-file-using-file-get-contents
define('MULTIPART_BOUNDARY', '--------------------------'.microtime(true));
$header = 'Content-Type: multipart/form-data; boundary='.MULTIPART_BOUNDARY;
// equivalent to <input type="file" name="uploaded_file"/>
define('FORM_FIELD', 'uploaded_file');
$filename = realpath(__FILE__);
$file_contents = file_get_contents($filename);
$content = "--".MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"".FORM_FIELD."\"; filename=\"".basename($filename)."\"\r\n".
"Content-Type: application/zip\r\n\r\n".
$file_contents."\r\n".
// add some POST fields to the request too: $_POST['foo'] = 'bar'
$content .= "--".MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"foo\"\r\n\r\n".
"bar\r\n".
// signal end of request (note the trailing "--")
$content .= "--".MULTIPART_BOUNDARY."--\r\n";
$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $header,
'timeout' => 30, # response timeout
'content' => $content,
),
));
$resource = file_get_contents('http://huypv.net/medias/perl/upload.cgi', false, $context);
var_dump($resource);
Title:
PHP - Upload with file_get_contents and context
Description:
<?php # The boundary can be any string that is not contained in the content body. I will usually use a timestamp # http://stackoverflow...
...
Rating:
4