按临时链接下载文件(download.php)
此文件通过临时下载链接下载文件。
- 从URL的查询字符串获取文件ID和键。
- 从键获取时间并计算链接到期时间。
- 从令牌文件中检索键。
- 循环通过键查找匹配,当找到匹配时,将其删除。
- 将剩余的键放回令牌文件中。
- 如果找到匹配并且链接未到期,则强制浏览器下载该文件。
<?php //Include the configuration file require_once 'config.php'; //Get the file ID & key from the URL $fid = base64_decode(trim($_GET['fid'])); $key = trim($_GET['key']); //Calculate link expiration time $currentTime = time(); $keyTime = explode('-',$key); $expTime = strtotime(EXPIRATION_TIME, $keyTime[0]); //Retrieve the keys from the tokens file $keys = file(TOKEN_DIR.'/keys'); $match = false; //Loop through the keys to find a match //When the match is found, remove it foreach($keys as &$one){ if(rtrim($one)==$key){ $match = true; $one = ''; } } //Put the remaining keys back into the tokens file file_put_contents(TOKEN_DIR.'/keys',$keys); //If match found and the link is not expired if($match !== false && $currentTime <= $expTime){ //If the file is found in the file's array if(!empty($files[$fid])){ //Get the file data $contentType = $files[$fid]['content_type']; $fileName = $files[$fid]['suggested_name']; $filePath = $files[$fid]['file_path']; //Force the browser to download the file if($files[$fid]['type'] == 'remote_file'){ $file = fopen($filePath, 'r'); header("Content-Type:text/plain"); header("Content-Disposition: attachment; filename=\"{$fileName}\""); fpassthru($file); }else{ header("Content-Description: File Transfer"); header("Content-type: {$contentType}"); header("Content-Disposition: attachment; filename=\"{$fileName}\""); header("Content-Length: " . filesize($filePath)); header('Pragma: public'); header("Expires: 0"); readfile($filePath); } exit; }else{ $response = 'Download link is not valid.'; } }else{ //If the file has been downloaded already or time expired $response = 'Download link is expired.'; } ?> <html> <head> <title><?php echo $response; ?></title> </head> <body> <h1><?php echo $response; ?></h1> </body> </html>
配置(config.php)
配置变量在此文件中定义。
- '$files' - 具有唯一ID的文件数组。
- 我们可以指定正在下载的文件的不同名称。它有助于保护原始文件。
- 我们可以指定本地或者远程文件路径。
- 'base_url' - 定义应用程序的URL。
- 'download_path' - 定义文件下载脚本的路径。
- 'token_dir' - 设置将存储键的令牌目录路径。
- 'oauth_password' - 设置身份验证密码以生成下载链接。
- 'expiration_time' - 设置文件将过期的时间。
<?php //Array of the files with an unique ID $files = array( 'UID12345' => array( 'content_type' => 'application/zip', 'suggested_name' => 'codex-file.zip', 'file_path' => 'files/test.zip', 'type' => 'local_file' ), 'UID67890' => array( 'content_type' => 'audio/mpeg', 'suggested_name' => 'music-codex.mp3', 'file_path' => 'https://www.dropbox.com/XXXXXXX/song.mp3?dl=1', 'type' => 'remote_file' ), ); //Base URL of the application define('BASE_URL','http://'. $_SERVER['HTTP_HOST'].'/'); //Path of the download.php file define('DOWNLOAD_PATH', BASE_URL.'download.php'); //Path of the token directory to store keys define('TOKEN_DIR', 'tokens'); //Authentication password to generate download links define('OAUTH_PASSWORD','onitroad'); //Expiration time of the link (examples: +1 year, +1 month, +5 days, +10 hours) define('EXPIRATION_TIME', '+5 minutes');
index.php.
在此文件中,将显示一个链接以导航到下载链接创建文件。
需要在链接的查询字符串中指定身份验证密码。
<a href="generate.php?onitroad">Generate download link</a>
使用一次性下载链接,我们无需手动监控下载活动以更改下载链接。
相反,下载后的下载链接将在第一次下载后立即过期。
一次性下载链接常用于下载数字产品或者文件,在下载产品后马上使链接过期。
将遵循以下过程以实现PHP中的临时下载链接功能。
- 使用唯一键生成下载链接。
- 创建受保护目录以存储键。
- 创建文件并为其写一个唯一的键。
- 在下载请求中,验证令牌键和到期时间。
- 强制浏览器下载文件。
创建临时下载链接(Generate.php)
此文件创建临时下载链接,并列出网页上的链接。
查询字符串必须具有身份验证密码,并且需要与“config.php”文件中的指定匹配,否则,呈现404个错误。
- 从查询字符串获取身份验证密码。
- 验证身份验证密码。
- 将文件ID与PHP中的Base64_Encode()编码。
- 使用PHP中的UNIQID()生成新的唯一键,其中包含Uniqid()。
- 生成带有文件ID和键的下载链接。
- 创建受保护目录以存储键。
- 在文件中写下密钥并将其放在令牌目录中。
- 列出网页中的所有下载链接。
<?php //Include the configuration file require_once 'config.php'; //Grab the password from the query string $oauthPass = trim($_SERVER['QUERY_STRING']); //Verify the oauth password if($oauthPass != OAUTH_PASSWORD){ //Return 404 error, if not a correct path header("HTTP/1.0 404 Not Found"); exit; }else{ //Create a list of links to display the download files $download_links = array(); //If the files exist if(is_array($files)){ foreach($files as $fid => $file){ //Encode the file ID $fid = base64_encode($fid); //Generate new unique key $key = uniqid(time().'-key',TRUE); //Generate download link $download_link = DOWNLOAD_PATH."?fid=$fid&key=".$key; //Add download link to the list $download_links[] = array( 'link' => $download_link ); //Create a protected directory to store keys if(!is_dir(TOKEN_DIR)) { mkdir(TOKEN_DIR); $file = fopen(TOKEN_DIR.'/.htaccess','w'); fwrite($file,"Order allow,deny\nDeny from all"); fclose($file); } //Write the key to the keys list $file = fopen(TOKEN_DIR.'/keys','a'); fwrite($file, "{$key}\n"); fclose($file); } } } ?> <!-- List all the download links --> <?php if(!empty($download_links)){ ?> <ul> <?php foreach($download_links as $download){ ?> <li><a href="<?php echo $download['link']; ?>"><?php echo $download['link']; ?></a></li> <?php } ?> </ul> <?php }else{ ?> <p>Links are not found...</p> <?php } ?>
日期:2020-06-02 22:15:30 来源:oir作者:oir