Skip to content

Instantly share code, notes, and snippets.

@davidperezgar
Created June 22, 2022 06:23
Show Gist options
  • Save davidperezgar/4f41b7de10d2e936e879f64c7ffa316d to your computer and use it in GitHub Desktop.
Save davidperezgar/4f41b7de10d2e936e879f64c7ffa316d to your computer and use it in GitHub Desktop.
Force download file with Get option and generates GA event
<?php
// ---------------------------------
// File: download.php
// Author: Jim Penaloza
// Web: http://blog.unijimpe.net
// ---------------------------------
// verify file
if (!isset($_GET['file']) || empty($_GET['file'])) {
exit();
}
// get filename
$root = "";
$file = basename($_GET['file']);
$path = $root.$file;
$type = '';
/* Google Analytics */
include 'ss-ga.class.php';
$ssga = new ssga( 'UA-XXXXX-XXX', 'domain.com' );
$ssga->set_event( 'Category', 'Tag', $file, '10' );
$ssga->send();
if (is_file($path)) {
$size = filesize($path);
if (function_exists('mime_content_type')) {
$type = mime_content_type($path);
} else if (function_exists('finfo_file')) {
$info = finfo_open(FILEINFO_MIME);
$type = finfo_file($info, $path);
finfo_close($info);
}
if ($type == '') {
$type = "application/force-download";
}
// Set Headers
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=\"$file\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $size);
// Download File
readfile($path);
} else {
die("File not exist !!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment