doudilin1225 2015-05-21 08:18
浏览 225

PHP生成下载链接

Let's say i want to generate a download link and put it into <a> tag.

my php script:

function download_link(){
    $this_id = "d"; //this is the name of file from server
    $original_filename = 'xample.pdf'; //This come from database
    $ext = pathinfo($original_filename, PATHINFO_EXTENSION);

    $file = '../uploads/'.$this_id.'.'.$ext;
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/'.$ext);
        header('Content-Disposition: attachment; filename='.$original_filename);//Rename the file with its original filename
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        return readfile($file);//Here where i want  to return the generated url
    }                       
    return '#'; //Or return nothing if file doesn't exist

   echo '<a href="'.function download_link().'"></a>'; //And put it here, the generated url

now, my directory location is ../uploads/.
i am expecting a result like: <a href="www.myserver.com/whatever the new url is"></a> so when the user click this tag the file will be downloaded. but instead, when i reload the page it is automatically downloading without clicking the download button which is the <a> tag.

note: i am trying to rename the filename when botton download is clicked. i know there is a problem in my logic. maybe this can be done with JQUERY? or AJAX? im searching for solution but did not find the answer.

here's what i did with JQUERY AJAX:
HTML tag
<a id="server_name_file_name">download</a>

JQUERY AJAX:

$('a').click(function(e) {
   e.preventDefault();
   var id = $(this).attr('id');
   $.ajax({
            type: 'POST',  
            url: 'download.php', 
            data: { server_file_name: id,},
            success: function(response) {
                if(response == 1){
                    alert("Unable to download, Maybe the file is corrupted. Please try to reload the page.");
                }else{
                    window.location.href = response;
                    return false;
                }
            }})

});

download.php

$this_id = $_POST['server_file_name'];
$original_filename = 'xample.pdf'; //This come from database
$ext = pathinfo($original_filename, PATHINFO_EXTENSION);

    $file = '../uploads/'.$this_id.'.'.$ext;
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/'.$ext);
        header('Content-Disposition: attachment; filename='.$original_filename);//Rename the file with its original filename
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        echo readfile($file);//Here where i want  to return the generated url
        exit();
    }  die('1');

but doesnt work.
anyone can help me here?
Thank you!!!!

  • 写回答

2条回答 默认 最新

  • douqin6785 2015-05-21 08:27
    关注

    You're returning the actual contents of the file with readfile. Thats why browser starts to download the file you return.

    What you need to do is to generate the string which will point to the file. If your "uploads" dir is accessible by url, then your downloads.php should look like this:

    $this_id = $_POST['server_file_name'];
    $original_filename = 'xample.pdf'; //This come from database
    $ext = pathinfo($original_filename, PATHINFO_EXTENSION);
    
    $file = '../uploads/' . $this_id . '.' . $ext;
    if (file_exists($file)) {
        echo 'www.myserver.com/uploads/' . $this_id . '.' . $ext;
        exit();
    } 
    die('1');
    

    If your uploads dir is not accessible from outside, then you need to copy the file into the public directory first.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配