更改文件名下载,点击或延迟后开始下载 [英] Change file name for download and start downloading it after click or delay

查看:135
本文介绍了更改文件名下载,点击或延迟后开始下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找更改文件名称-add当前日期的php代码,并开始下载文件延迟。如果下载不能开始,可以通过点击链接下载添加日期的文件。

I'm looking for php code that changes file name -adds current date, and starts download the file with delay. If download will not start there is an option to download the file with added date by clicking the link.

这样的一个例子:
您的下载将以几分钟...如果没有发生,点击< a href =>这里< / a>

我发现只有这样:

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('plik.pdf');

https:/ /stackoverflow.com/a/6694505

请帮助我。

推荐答案

您将需要两个部分来有效地执行此操作:在显示的PHP文件(我们称之为 download.php )中,您需要启动一个JavaScript功能,使您的客户倒数为零。当它达到零时,它只是重定向到真正的下载位置(让我们称之为 realdl.php )。此文件实际上会抓取文件内容,并在重定向或点击时将其发送给用户。

You will need two parts to do this effectively... In your displayed PHP file (let's call it download.php), you'll need to kick off a Javascript function that counts down for your customer to zero. When it reaches zero, it simply redirects to the real download location (let's call it realdl.php). This file would actually grab the file content and send it to the user when either redirected or clicked.

以下是下载中需要的一些元素.php

<? $file_dl_url = "/realdl.php?id=FILEID"; ?>

<script language="javascript">
  var elapsed = 0;
  function countdown {
    // see if 5 seconds have passed
    if (elapsed >= 5) {
      window.location = <?= $file_dl_url ?>;
    } else {
      // update countdown display & wait another second
      elapsed++;
      setTimeout("countdown", 1000);
    }
  }
  setTimeout("countdown", 1000);
</script>

<a href="<?= $file_dl_url ?>">Click Here</a>

然后,您将需要 realdl.php 是以下内容:

Then, all you would need in realdl.php is the following:

$file_contents = load_file_from_id($_GET['id']);
$file_name = determine_filename();

header("Content-Disposition: attachment; filename=$file_name");

echo $file_contents;

当然,您需要提供方法来获取文件内容(只需从磁盘或可能的数据库)以及确定文件名。要使用时间作为文件名格式,请参阅 http://us3.php.net/手动/ en / function.strftime.php strftime 函数。

Of course, you need to provide the methods to get the file contents (either just read from disk or possibly database) as well as to determine the file name. To use time as a filename format, see http://us3.php.net/manual/en/function.strftime.php for the strftime function.

根据文件被存储,您可以使用 fpassthru 作为本地文件更有效,例如。您还可以发送 Content-Length 标题,如果您可以在下载前确定文件大小(即,您正在发送的静态内容)。

Depending on how the file is stored, you can be more effective, using fpassthru for local files, as an example. You also may want to send the Content-Length header if you can determine the file size prior to downloading (i.e. it is static content you are sending).

这篇关于更改文件名下载,点击或延迟后开始下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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