<?php
// Anonymous function (PHP 5.3+). See http://stackoverflow.com/questions/627775/php-pass-function-as-param-then-call-the-function
$appendChunkBySoap = function ($file_name, // File name to pass to TP 'AppendChunk' SOAP endpoint.
$buffer, // Buffer to pass to TP 'AppendChunk' SOAP endpoint.
$offset, // Offset to pass to TP 'AppendChunk' SOAP endpoint.
$bytes_read) { // BytesRead to pass to TP 'AppendChunk' SOAP endpoint.
// Code to form & send SOAP request to TP 'AppendChunk' SOAP endpoint.
// If any error occurs, return FALSE.
// echo "Can't append chunk to " . $file_name . " , error: " . error_message;
// return FALSE;
// return TRUE if all is ok.
return TRUE;
};
function uploadAttachmentByChunks($chunk_appender, // TP attacment upload function.
$attachment_path, // Path to attachment file.
$chunk_size) { // Size of chunk to upload at once.
$file_name = basename($attachment_path);
$attachment_handle = fopen($attachment_path, "rb");
if (FALSE === $attachment_handle) {
echo "Failed to open attachment: " . $attachment_path . "\n";
return FALSE;
}
$buffer = '';
$offset = 0;
$bytes_read = 0;
$is_chunk_appended = TRUE;
while (!feof($attachment_handle) && $is_chunk_appended === TRUE) {
$buffer = fread($attachment_handle, $chunk_size);
$bytes_read = strlen($buffer);
$is_chunk_appended = $chunk_appender($file_name,
$buffer,
$offset,
$bytes_read);
$offset += $bytes_read;
}
fclose($attachment_handle);
return $is_chunk_appended;
}
// Upload attachment 'my_useful_attachment.zip' by 16384 bytes (16 Kb) chunks.
$upload_result = uploadAttachmentByChunks($appendChunkBySoap,
"my_useful_attachment.zip",
16384);
echo "Attachment 'my_useful_attachment.zip' is " .
($upload_result ? "": "NOT ") .
"added to my TP attachments." . "\n";
?>