note 99669 added to function.http-put-file
| From: | D' | Date: | Mon, 30 Aug 2010 11:13:47 +0000 |
| Subject: | note 99669 added to function.http-put-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-172156@lists.php.net to get a copy of this message | ||
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");
?>
----
Server IP: 212.150.176.151
Probable Submitter: 212.179.137.34
----
Manual Page -- http://www.php.net/manual/en/function.http-put-file.php
Edit -- https://master.php.net/note/edit/99669
Del: integrated -- https://master.php.net/note/delete/99669/integrated
Del: useless -- https://master.php.net/note/delete/99669/useless
Del: bad code -- https://master.php.net/note/delete/99669/bad+code
Del: spam -- https://master.php.net/note/delete/99669/spam
Del: non-english -- https://master.php.net/note/delete/99669/non-english
Del: in docs -- https://master.php.net/note/delete/99669/in+docs
Del: other reasons-- https://master.php.net/note/delete/99669
Reject -- https://master.php.net/note/reject/99669
Search -- https://master.php.net/manage/user-notes.php