带有短URL的文件以原始文件名下载 [英] File with a short URL downloaded with original filename

查看:59
本文介绍了带有短URL的文件以原始文件名下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题(请参阅下面的详细说明和我已经尝试过的东西):

是否可以通过短网址 http://example.com/qf56p9z 进行重定向到服务器上找到的格式为/files/qf56p9z _(.*)的第一个文件,并以客户端文件名= (.*)?(如果可能,没有PHP)

Is there a way of having the short URL http://example.com/qf56p9z redirect to the first file found on the server of the form /files/qf56p9z_(.*), and having it downloaded on the client with filename = (.*) ? (if possible, without PHP)

背景:

我在服务器上有一个文件,该文件存储在/files/qf56p9z_blahblah.xls 中,我希望可以通过以下简短网址进行访问:

I have a file on server stored in /files/qf56p9z_blahblah.xls that I would like to be accessible from this short URL :

http://example.com/qf56p9z

(与其他数千个文件相同)

(+the same for other thousands of files)

此外,我希望当用户转到该URL时,该文件将使用原始文件名(即 blahblah.xls )下载到客户端计算机上.

Moreover, I would like that when the user goes to this URL, the file will be downloaded on the client computer with the original filename, i.e. blahblah.xls.

使用PHP可能会发生这种情况,我尝试过类似的操作(对于我成千上万个文件中的每一个都可以轻松实现自动化):

<?php
header('Content-type: ...');
header('Content-Disposition: attachment; filename="blahblah.xls"');
readfile('qf56p9z_blahblah.xls');
?>

但是我看到这种方法有两个问题:

But I see two problems with this method :

  • 对于客户端每次读取文件,PHP必须将整个文件加载到内存中,然后输出新文件.与apache仅发送文件而不使用PHP相比,这会消耗更多的CPU/内存.

  • For each reading of the file by a client, PHP has to load the whole file in memory, and output the new file. This is much more CPU/memory consuming than if apache has just to send the file, without PHP

如果有一天我将所有这些文件托管在其他地方(例如在Amazon S3上),则该文件将必须遵循以下路线:

If one day I host all those files elsewhere (on Amazon S3 for example), the file will have to do this route :

远程托管(AmazonS3)==​​>我的用PHP ==>修改的服务器/标头;客户

可惜的是数据必须执行此操作,而不是:

It is a shame that the data has to do this instead of :

远程托管(AmazonS3)==​​>客户

推荐答案

您可以结合使用 PHP .htaccess (使用mod_rewrite )

  • /var/www/html/files/ :目录,其中包含您的文件 {prefix} _ {filename} 格式
  • /var/www/html/download.php :PHP脚本来查找具有给定前缀的第一个文件
  • /var/www/html/.htaccess :带有2个用于重写下载URL的mod_rewrite规则

download.php

download.php

<?PHP

$files_dir = "files/";

if(isset($_GET['prefix'])){
    $files = glob($files_dir.$_GET['prefix']."_*");
    if(count($files) > 0) {
        $dl = strlen($files_dir);
        $pl = strlen($_GET['prefix']);
        $fn = substr($files[0], $dl+$pl+1);
        header("Location: d/".$_GET['prefix']."/".$fn);
    }
}

?>

这将选择具有所需前缀的第一个文件并重定向到该文件.

This selects the first file with the required prefix and redirects to it.

.htaccess

.htaccess

RewriteEngine On
RewriteRule ^(\w+)$     download.php?prefix=$1 [L]
RewriteRule ^d/(.+)/(.+)$   files/$1_$2 [L]

第一条规则将短URL重写为download.php,第二条规则将URL重写为原始文件位置.

The first rule rewrites the short URL to download.php and the second rule rewrites the URL to the original file location.

这是它的工作方式

  1. http://example.com/qf56p9z - .htaccess -> http://example.com/download.php?prefix=qf56p9z
  2. download.php- PHP重定向-> http://example.com/d/qf56p9z/blahblah.xls
  3. http://example.com/d/qf56p9z/blahblah.xls -- .htaccess -> http://example.com/files/qf56p9z_blahblah.xls
  1. http://example.com/qf56p9z -- .htaccess -> http://example.com/download.php?prefix=qf56p9z
  2. download.php -- PHP Redirect --> http://example.com/d/qf56p9z/blahblah.xls
  3. http://example.com/d/qf56p9z/blahblah.xls -- .htaccess --> http://example.com/files/qf56p9z_blahblah.xls

在上述所有步骤中,浏览器的位置栏URL保持不变,没有任何更改.

During all the above steps, browser's location bar URL remains the same without any change.

这篇关于带有短URL的文件以原始文件名下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆