PHP文件下载功能 在ThinkPHP中实现步骤

 下载功能

1.如果是其他文件,直接在a标签的href属性中填写路径,可以下载。

2.如果是一个图片文件,那么直接在a标签的href属性中,填写路径是不能进行下载。

那么需要进行一下操作步骤:

         PHP文件下载固定4句话:
    
header("Content-type: application/octet-stream");
header('Content-Disposition: attachment; filename="'. basename($path) .'"');
header("Content-Length: ". filesize($path));
readfile($path);
         所需的参数是:文件路径 

思路:创建Doc/download方法,在方法中获取要下载的文件路径,调用上面4句话进行下载 

1) 修改文件下载地址,将文件的doc_id传递到download方法中 


2) 创建Doc/download方法,接收doc_id,根据id查询对应的路径,再调用4句下载函数,进行下载操作。

function download(){
    //接收公文id
    $id = I('get.id');
    //根据公文id查询对应的附件路径
    $data = D('Doc')->field('doc_file')->find($id);
    $path = $data['doc_file'];

    header("Content-type: application/octet-stream");
    header('Content-Disposition: attachment; filename="'. basename($path) .'"');
    header("Content-Length: ". filesize($path));
    readfile($path);
}


猜你喜欢

转载自blog.csdn.net/lw545034502/article/details/79469939