首页 > php或html网页怎么做是下载文件,怎么做是打开文件?

php或html网页怎么做是下载文件,怎么做是打开文件?

php或html网页,上面有个文件链接,点击它的时候,怎么样做是让这个文件打开,怎么样做是让它下载?就是出现那个下载对话框。


这样是进行文件的下载:

function downloadfile($filename,$storage) {
    if(! is_file($storage)) exit('文件不存在!');
    if(!$fp = fopen($storage, 'r')) exit('error');

    header('Content-Type: application/octet-stream;charset=utf-8');
    if(preg_match("/Trident/", $_SERVER["HTTP_USER_AGENT"])) {
        header('Content-Disposition: attachment; filename="'.utf8togbk($filename).'"');
    } else {
        header('Content-Disposition: attachment; filename="'.$filename.'"');
    }
    header('Content-Transfer-Encoding: binary');

    while(! feof($fp)){
        echo fread($fp,1025);
    }
    fclose($fp);
}

打开文件应该是浏览器的行为吧?


通过php的header()方法,来修改html头部内容,来达到目的

【热门文章】
【热门文章】