Australia's Leading Digital Marketing Experts. T. 1300 235 433  |  Aggregation Enquires Welcome

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.

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Leave a comment