2010-07-10 62 views
5

我想创建一个PHP网页,其中显示了类似如何创建一个PHP'即将开始下载'页面?

Your download will begin shortly. 

If it does not start, please click here to restart the download 

即同一类型的页面上各大网站存在的消息。

它会像这样:

<a href="download.php?file=abc.zip">Click here</a> 

当用户点击该链接,他导致的download.php这说明他的消息,然后提供文件下载。

我该怎么做?

非常感谢!

回答

2

链接需要做两件事情之一:直接

    • 指向文件的Web服务器上的PHP脚本,它会做什么,但设置相应的头文件和提供服务的文件作为页面主体。没有文字输出!有关如何实际提供文件的信息,请参见http://teddy.fr/blog/how-serve-big-files-through-php

    让浏览器自行启动下载的一种方法是使用META REFRESH标签。

    另一种方法是使用JavaScript,像这样的(来自Mozilla的Firefox下载页面):

    function downloadURL() { 
        // Only start the download if we're not in IE. 
        if (download_url.length != 0 && navigator.appVersion.indexOf('MSIE') == -1) { 
         // 5. automatically start the download of the file at the constructed download.mozilla.org URL 
         window.location = download_url; 
        } 
    } 
    
    // If we're in Safari, call via setTimeout() otherwise use onload. 
    if (navigator.appVersion.indexOf('Safari') != -1) { 
        window.setTimeout(downloadURL, 2500); 
    } else { 
        window.onload = downloadURL; 
    } 
    
  • +0

    例如看到这个页面: http://www.mozilla.com/en-US/products/download.html? product = firefox-3.6.6&os = win&lang = en-US 这样就有了文本输出,但文件却在下载。我想重复这样的东西。 – Rohan 2010-07-10 05:19:22

    +0

    太好了,谢谢你的更新回答!我感觉合理:-) – Rohan 2010-07-10 05:30:59

    2
    <?php 
    // download.php 
    $url = 'http://yourdomain/actual/download?link=file.zip'; // build file URL, from your $_POST['file'] most likely 
    ?> 
    <html> 
        <head> 
         <!-- 5 seconds --> 
          <meta http-equiv="Refresh" content="5; url=<?php echo $url;?>" /> 
        </head> 
        <body> 
         Download will start shortly.. or <a href="<?php echo $url;?>">click here</a> 
        </body> 
    </html> 
    
    0

    如果你想确保该文件就会被下载(而不是在所示浏览器或浏览器插件),您可以设置Content-Disposition HTTP标头。例如,强制PDF文件下载,而不是在浏览器插件开盘:

    header('Content-type: application/pdf'); 
    header('Content-Disposition: attachment; filename="foo.pdf"'); 
    readfile('foo.pdf'); 
    
    相关问题