douxianliu6756 2013-08-29 05:32
浏览 144

使用PHP为上传的文件生成下载链接

Using PHP, I have created a form which uploads and sends files via email. Now, the upload works perfectly and automatically sends an email to a specified email address. I'd like to include, if it's possible a download link for the file inside the email. I am able to send a link already but whenever I click on the link, all it does it open the file on the browser. I'd like it download when the link is clicked instead. The thing is, I am not connecting or saving the uploaded file to my database. I'm just uploading it on my server and sending it directly to the receiver. Is it possible to achieve what I'd like to do? If it is, how can I achieve this? Or do I need to create a database for it?

  • 写回答

1条回答 默认 最新

  • dongxiangqian1855 2013-08-29 05:39
    关注

    You need to write file download code on that page where your link is pointing. From link extract the file name and write the following code

     if (file_exists($file))
     {
        if (FALSE!== ($handler = fopen($file, 'r')))
       {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: chunked'); //changed to chunked
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        //header('Content-Length: ' . filesize($file)); //Remove
    
        //Send the content in chunks
        while(false !== ($chunk = fread($handler,4096)))
        {
            echo $chunk;
        }
    }
    exit;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线