Last active
January 3, 2016 05:28
-
-
Save tomookaku/8415648 to your computer and use it in GitHub Desktop.
zipファイルのダウンロード(更新確認付)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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