Tuesday, October 21, 2008

How to use Attachment Caching with WSF/PHP

WSF/PHP 2.0.0 has the support for caching attachments ( writing to a file ). This effectively reduces the amount of memory used when sending and receiving attachments and its specially useful, if your application requires to send or receiving a very large file in the scale of megabytes.

WSF/PHP adds two php.ini entries in order to allow attachment caching.

1. wsf.attachment_cache_dir

2.wsf.enable_attachment_caching

wsf.attachment_cache dir is the location where the received attachments will be saved.

wsf.enable_attachment_caching option enables attachment caching.

By default, attachment caching can be done only for attachments larger than 1 MB.

Lets see a code sample on how to use this. This is a simple php service, which reads a binary file and send it to the client as an MTOM attachment.

ini_set("wsf.enable_attachment_caching", 1);
ini_set("wsf.attachment_cache_dir","E:\\");

function sendAttachment($msg)
{
$responsePayloadString = << http://php.axis2.org/samples/mtom">
test.jpg
http://www.w3.org/2004/06/xmlmime">
http://www.w3.org/2004/08/xop/include" href="cid:myid1">


XML;

$responseMessage = new WSMessage($responsePayloadString,
array( "attachments" => array("myid1" => "../resources/large_image.jpg")));
return $responseMessage;
}

$operations = array("download" => "sendAttachment");

$service = new WSService(array("operations" => $operations, "useMTOM" => TRUE));

$service->reply();

?>

Now in above code, I have highlighted the important code pieces.

1. The php ini settings at the top of the code where the attachment_cache_dir is set and attachment caching is enabled.

ini_set("wsf.enable_attachment_caching", 1);
ini_set("wsf.attachment_cache_dir","E:\\");

2. Setting of a fake content id to which the actual attachment is assigned.

href="cid:myid1"

3. Instead of setting binary attachment as an string in the attachment array, we should set the actual path to the file.

array("myid1" => "../resources/large_image.jpg")

Thats all you need to do to get attachment caching working. The same technique can be used for the client as well. It will be consuming lot less amount of memory.


http://phpwebservices.blogspot.com/2008/10/how-to-use-attachment-caching-with.html

3 comments:

Anonymous said...

PECS is Microsoft & ISO 9001 certified company that provide new media
solutions all across the globe having sales office in UK and India. We
provide solutions related Web Design & Development, SEO and Bespoke
Software for all business domains.
Web Design Company UK, web development company UK, Web development company, website design company, UK, india, Website design india, Offshore website Development, Custom Web Applications, Software developments, webs developments Services uk, Indian Web design Company, Application software development, outsourcing services, affordable webs designs services

Akash said...

Don't know much about PHP but i heard it from my one friend who is web developer. he told me that PHP is advanced language of web development.

Web Development India said...

Thank you for appreciating
In this day and age, nobody can undermine the importance of website development.