php 文件下载实现功能实例

用在服务器上提供下载的PHP代码,可以指定被下载的文件名,可以动态指定文件内容PHP处理文件下载的代码,感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
经测试代码如下:

/**
 * 文件下载功能
 *
 * @param 
 * @arrange (编程之家) jb51.cc
 **/
$local_file = 'test.zip';
$download_file = 'your-download-name.zip';
 
if(file_exists($local_file) && is_file($local_file)) {
    // send headers
    header('Cache-control: private');
    header('Content-Type: application/octet-stream'); 
    header('Content-Length: '.filesize($local_file));
    header('Content-disposition: filename='.$download_file);
 
    // flush content
    flush();
 
    /*
    ** You can also remove the following part and
    ** replace it trough a database command fetching 
    ** the download data
    */
    // open file stream
    $file = fopen($local_file,rb);
 
    // send the file to the browser
    print fread ($file,filesize($local_file)); 
 
    // close file stream
    fclose($file);}
else {
    die('Error: The file '.$local_file.' does not exist!');
}


/***   代码来自编程之家 jb51.cc(jb51.cc)   ***/

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...