The Original FileMaker Community
Free Templates - Demo Apps - Video Tutorials -Samples - Help - 46000 Member Forum

The Leading Filemaker Developer Tools

Password protection and security…

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.




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17

// create the zip archive 
$zip = new ZipArchive();
$zipname = 'records_' . date('U');
$filename = '/tmp/' . $zipname . '.zip';

if ($zip->open($filename, ZipArchive::CREATE) !== TRUE) {
    exit("cannot open <$filename>n");
}

foreach ($fm_records as $value) {

    $the_file = base64_decode($value->getField('File_b64'));
    $the_file_name = $value->getField('File_name');
    $zip->addFromString($the_file_name, $the_file);
}

$zip->close();


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.


Auto Download

One click and all container data is downloaded!


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:




1
2

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . ""$file_name"");


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.”


References


  1. FileMaker WebDirect: http://www.filemaker.com/products/filemaker-server/webdirect.html

  2. PHP Zip Documentation: http://php.net/manual/en/book.zip.php

  3. FileMaker 13 – Working with Base 64: http://www.soliantconsulting.com/blog/2014/06/filemaker-13-working-base-64

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy