Over 46,000+ Business Solution Developers Find answers, ask questions, and connect with our community of business solutions developers, business owners and partners.
Extending FileMaker Webdirect: Automatically Zip Container Fields – FileMaker Today
The Use Case
Say you have several container fields associated with a single record and you want to get all those related files at once. You would like it in one nice, convenient zip file that automatically downloads when you click a button. In the standard interface, you would need to download each file individually. We can do better.
By combining a little Custom Web Publishing (CWP) with WebDirect, we can extend the capabilities and provide some added functionality for our users.
Using the FileMaker API for PHP, we can retrieve all records with container data, zip them all in one archive and automatically have the users web browser download it.
You know, “I don’t want to click on a bunch of things, I want my stuff now…” 🙂
How It Works
We set the URL of a web viewer in WebDirect, so we can pass a record ID to our PHP script. This enables us to find the correct record in FileMaker from our PHP script. No problem there.
Now the interesting part. PHP already has built in zip functions, see http://php.net/manual/en/book.zip.php. All we need to do is feed it some data. To get at the container data, we take advantage of the Base 64 functions in FileMaker. There are two parts to each file, the contents of the file itself, which we get in base 64, and the file name. Both of these are something we can calculate from container data.
First, we instantiate a zip archive in the temp directory on the server. Then we loop through the found records and add them all to the zip archive, since there can potentially be multiple related records for the parent record. PHP has a function to decode the base 64 data (base64_decode) that we use to insert the data in the archive.
That is all that is needed, once we finish our foreach loop, all the data is now in our zip file. In this sample, I have a rather high limit of 1000 records found, which can be adjusted as needed, but should be sufficient for most uses.
Using PHP, we can adjust the HTTP headers to tell the users web browser to start automatically downloading the zip file. That way, there is no link to click on…no need to right click and do a “Save As…” in order to download the file. What makes this work, most importantly, are these two headers:
The variable “$file_name” is set earlier in the PHP script, so can be whatever we want. In this case, it is a generic “records_” and a date string to be somewhat unique, but it can be anything we like.
After the download is finished, we unlink the file in our PHP script, which removes the temporary file on the server. It is always good to clean up after yourself, if possible.
By extending the capabilities of WebDirect, there are more possible uses to apply it to. Additionally, providing a more convenient and intuitive user experience is well worth the effort. Those who use your application will appreciate it.
Get the files from GitHub:
(click the “Download Zip” link at the bottom of the right hand colomn.)
A Few Other Notes:
This example is meant to be open and easy enough to follow. If security is a concern, that could be addressed with some additional consideration.
Also, if SSL is being used in WebDirect, then of course the PHP script location should as well. If WebDirect’s URL begins with “HTTPS” then the PHP script should also be called from “HTTPS.”
FileMaker WebDirect: http://www.filemaker.com/products/filemaker-server/webdirect.html
PHP Zip Documentation: http://php.net/manual/en/book.zip.php
FileMaker 13 – Working with Base 64: http://www.soliantconsulting.com/blog/2014/06/filemaker-13-working-base-64