0

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?

4

1 回答 1

1

您需要在链接指向的页面上编写文件下载代码。从链接中提取文件名并编写以下代码

 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;
}
于 2013-08-29T05:39:41.537 回答