Change file name for download and start downloading it after click or delay

谁都会走 提交于 2020-01-13 16:44:09

问题


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.

Something like this: Your download will begin in a few moments... If nothing happens, click <a href="">here</a>.

I found only this:

// 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

Please help me.


回答1:


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.

Here are some elements you would need in download.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>

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;

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.

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).




回答2:


I do not recommend using sleep or other delays on server side (PHP). You could use some kind of JavaScript code which will display the desired notification to the user and redirect the page to file download URL (PHP script). There you can use Content-Disposition header to change the name of the file being downloaded.

The JavaScript code could be generated partially with PHP if necessary to get the URL to be redirected to.




回答3:


You can try add some time-date variables such as:

$delay = 10; #delay of 10 seconds
sleep($delay);

$timedt = time()+$delay;

$filename = 'downloaded-'.$timedt.'.pdf';

header('Content-Disposition: attachment; filename="{$filename}"');

Improvise and add some functionallty for checking on conditionals (if/else) time-date checkeres than set a sleep. Add on a filename for download.



来源:https://stackoverflow.com/questions/12877160/change-file-name-for-download-and-start-downloading-it-after-click-or-delay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!