Skip to content

Instantly share code, notes, and snippets.

@tomookaku
Last active January 3, 2016 05:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomookaku/8415648 to your computer and use it in GitHub Desktop.
Save tomookaku/8415648 to your computer and use it in GitHub Desktop.
zipファイルのダウンロード(更新確認付)
<?php
define('DOWMNLOAD_PATH', '/home/download/'); // download file path
$time = time();
$filename = isset($_REQUEST['name']) ? $_REQUEST['name'] : null;
if ($filename == null) {
header("HTTP/1.0 404 Not Found");
exit;
}
$hash = isset($_REQUEST['hash']) ? $_REQUEST['hash'] : '';
if (!file_exists(DOWMNLOAD_PATH.$filename)) {
header("HTTP/1.0 404 Not Found");
exit;
}
$file = File::file_info($download_file_path.$filename);
$redis = new Redis();
$files = $redis->get('download-files');
if ($files && isset($files[$filename]))
$file_info = $files[$filename];
else
$file_info = false;
if ($files && $file_info && ($file['time_modified'] == $file_info['time'] && $hash && ($hash == $file_info['hash']))) {
header("HTTP/1.0 304 Not Modified");
exit;
}
$hash = hash_file("md5", $download_file_path.$filename, false);
$file_info = array('time' => $file['time_modified'], 'hash' => $hash);
$files[$filename] = $file_info;
$redis->set('download-files', $files);
header("X-Download-Hash: " . $hash);
header("Content-Type: application/zip");
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: '.filesize($download_file_path.$filename));
readfile($download_file_path.$filename);
$handle = fopen($path, "rb");
while(!feof($handle))
{
print fread($handle, 4096);
ob_flush();
flush();
}
fclose();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment