http_put_file
(PECL pecl_http >= 0.10.0)
http_put_file — Perform PUT request with file
Description
string http_put_file
( string
$url
, string $file
[, array $options
[, array &$info
]] )Performs an HTTP PUT request on the supplied url.
See the full list of request options.
Return Values
Returns the HTTP response(s) as string on
success, or FALSE
on failure.
[an error occurred while processing the directive]
- http_cache_etag
- http_cache_last_modified
- http_chunked_decode
- http_deflate
- http_inflate
- http_build_cookie
- http_date
- http_get_request_body_stream
- http_get_request_body
- http_get_request_headers
- http_match_etag
- http_match_modified
- http_match_request_header
- http_support
- http_negotiate_charset
- http_negotiate_content_type
- http_negotiate_language
- ob_deflatehandler
- ob_etaghandler
- ob_inflatehandler
- http_parse_cookie
- http_parse_headers
- http_parse_message
- http_parse_params
- http_persistent_handles_clean
- http_persistent_handles_count
- http_persistent_handles_ident
- http_get
- http_head
- http_post_data
- http_post_fields
- http_put_data
- http_put_file
- http_put_stream
- http_request_body_encode
- http_request_method_exists
- http_request_method_name
- http_request_method_register
- http_request_method_unregister
- http_request
- http_redirect
- http_send_content_disposition
- http_send_content_type
- http_send_data
- http_send_file
- http_send_last_modified
- http_send_status
- http_send_stream
- http_throttle
- http_build_str
- http_build_url
Коментарии
After much frustration and very little documentation that I could find, I thought I'd offer this example of implementation of http_put_file with custom HTTP headers, and the corresponding ReceiveFile.php.
I do an http put with a customized header containing parameters for file processing.
<?php
$header = array(
'file_size' => $file_size
, 'file_name' => $file_name
, 'md5sum' => $md5sum
);
$URI = 'http://MyDomain.com/ReceiveFile.php';
if (($f = @fopen($URI,'r'))) {
fclose($f);
if ($result = @http_put_file($URI, $file_path, array(
headers => array(
'X_CUSTOM_PUT_JSON' => json_encode($header)
,'X_FRUIT' => 'bananna'
)
, useragent => 'Magic UnitTests'
)
, $info))) {
echo str_replace("\n",'<BR>',$result);
}
else
echo 'http failure';
}
else
echo "Can't find URI: [$URI]";
?>
ReceiveFile.php has:
<?php
$CUSTOM_HEADER = 'HTTP_X_CUSTOM_PUT_JSON';
$CHUNK = 8192;
try {
if (!($putData = fopen("php://input", "r")))
throw new Exception ("Can't get PUT data.");
if (!(array_key_exists($CUSTOM_HEADER, $_SERVER)))
throw new Exception ("Custom header missing.")
$json = json_decode($_SERVER[$CUSTOM_HEADER], true);
$this->logParams(__FUNCTION__, $json);
foreach ($json as $fld => $val)
$$fld = $val;
// now the params can be used like any other variable
// see below after input has finished
$tot_write = 0;
// Create a temp file
if (!($tmpFileName = tempnam("/tmp", "PUT_FILE_")))
throw new Exception ("Can't create tmp file.");
// Open the file for writing
if (!($fp = fopen($tmpFileName, "w")))
throw new Exception ("Can't write to tmp file");
// Read the data a chunk at a time and write to the file
while ($data = fread($putData, $CHUNK)) {
$chunk_read = strlen($data);
if (($block_write = fwrite($fp, $data)) != $chunk_read)
throw new Exception ("Can't write more to tmp file");
$tot_write += $block_write;
}
if ( ! fclose($fp) )
throw new Exception ("Can't close tmp file");
unset($putData);
// Check file length and MD5
if ($tot_write != $file_size)
throw new Exception ("Wrong file size");
$md5_arr = explode(' ',exec("md5sum $tmpFileName"));
$md5 = $md5sum_arr[0];
if ($md5 != $md5sum)
throw new Exception ("Wrong md5");
?>