RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.20% (5.24%*) • Home Loan Fixed: 5.48% (6.24%*) • Fixed: 5.48% (6.24%*) • Variable: 5.20% (5.24%*) • Investment IO: 5.78% (6.81%*) • Investment PI: 5.49% (6.32%*)

Simple File Download Select Menu

This code is an example of how you might include multiple files of a particular type available in a select menu. On selecting the option the relevant zip file will be downloaded. Because we use it specifically for zip files, the browser handles the download without an intermediary file. If you were to use it for a page navigation menu (in which case you wouldn't be using the glob() function as we've done), or you had a directory with a deep hierarchy (in which case you might use something like our last function), only minor modifications would need to be made.

Because our use meant scanning a large directory, we cache the resulting select menu using Simple Cache.

The Result

The result is a standard select menu. Selecting a file will force the download (assuming you're listing zip files).

Simple File Download Select Menu

PHP Code

1
<?php 
2
include('../simple-cache/cache.inc.php');
3
 
4
/*
5
 Simple File Download Select Menu
6
 http://www.beliefmedia.com/code/php-snippets/file-download-select-menu
7
*/
8
 
9
function beliefmedia_download_form($dir = '/home/your/public_html/dir', $url = 'http://www.beliefmedia.com/something/', $type = 'zip', $cache = 43200) {
10
 
11
  /* Check for cached version */
12
  $transient = md5(serialize(func_get_args()));
13
  $cached = beliefmedia_get_transient($transient, $cache);
14
  if ($cached !== false) return $cached;
15
 
16
  /* JavaScript */
17
  $result = '<script type="text/javascript">
18
    function beliefmedia_bmemail_goto(select) {
19
      window.location = "' . rtrim($url, '/') . '/" + select.value;
20
    }
21
  </script>';
22
 
23
  /* Files to exclude from download list */
24
  $exclude = array('something.zip', 'some-other.zip');
25
 
26
  foreach (glob(rtrim($dir, '/') . '/*.' . $type) as $filename) {
27
    $filename = basename($filename);
28
    if (!in_array($filename, $exclude)) $result .= '<option value="' . $filename . '">' . $filename . '</option>';
29
  }
30
 
31
  $return = '<select name="bm_downloads" style="height: 30px; font-size: 1.0em;" onchange="beliefmedia_bmemail_goto(this)">';
32
  $return .= '<option value="0" SELECTED>Select File to Download</option>' . $result . '</select>';
33
 
34
  /* Cache result */
35
  beliefmedia_set_transient($transient, $return);
36
 
37
 return $return;
38
}
39
 
40
/* Usage */
41
echo beliefmedia_download_form();

Considerations

  • Other variations of this code are forthcoming. This includes a WordPress shortcode.
  • Information on glob() function from the PHP Manual .
  • We've got another cool function that we'll share soon that uses glob() to read all images from a directory and render then on a page in responsive rows (an easy way to create a quick image gallery).
  • To exclude specific files form being listed in the dropdown menu, include their name (with extension) in the $exclude array.

Download our 650-page guide on Finance Marketing. We'll show you exactly how we generate Billions in volume for our clients.

  E. Australia Standard Time [ UTC+10, Default ] [ CHECK TO CHANGE ]

  Want to have a chat?
 

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment