zoukankan      html  css  js  c++  java
  • php 下载保存文件保存到本地的两种方法

    php下载保存文件保存到本地的两种方法。

    第一种:

     1 <?php 
     2 function downfile()
     3 {
     4 $filename=realpath("resume.html"); //文件名
     5 $date=date("Ymd-H:i:m");
     6 Header( "Content-type: application/octet-stream "); 
     7 Header( "Accept-Ranges: bytes "); 
     8 Header( "Accept-Length: " .filesize($filename));
     9 header( "Content-Disposition: attachment; filename= {$date}.doc"); 
    10 echo file_get_contents($filename);
    11 readfile($filename); 
    12 }
    13 downfile();
    14 ?>

    <?php 
    //下载文件保存到本地
    //www.jbxue.com
    function downfile($fileurl)
    {
    ob_start(); 
    $filename=$fileurl;
    $date=date("Ymd-H:i:m");
    header( "Content-type: application/octet-stream "); 
    header( "Accept-Ranges: bytes "); 
    header( "Content-Disposition: attachment; filename= {$date}.doc"); 
    $size=readfile($filename); 
    header( "Accept-Length: " .$size);
    }
    $url="url地址";
    downfile($url);
    ?>

    第二种:

     1 <?php 
     2 //下载文件保存至本地
     3 //www.jbxue.com
     4 function downfile($fileurl)
     5 {
     6 $filename=$fileurl;
     7 $file = fopen($filename, "rb"); 
     8 Header( "Content-type: application/octet-stream "); 
     9 Header( "Accept-Ranges: bytes "); 
    10 Header( "Content-Disposition: attachment; filename= 4.doc"); 
    11 $contents = "";
    12 while (!feof($file)) {
    13 $contents .= fread($file, 8192);
    14 }
    15 echo $contents;
    16 fclose($file); 
    17 }
    18 $url="url地址";
    19 downfile($url);
    20 ?>
  • 相关阅读:
    Spring中配置和读取多个Properties文件
    Eclipse 常用快捷键
    static 静态导包
    CDN 备胎技巧
    org.apache.commons.lang3.ArrayUtils 学习笔记
    深入浅出 妙用Javascript中apply、call、bind
    URL详解与URL编码
    Chrome DevTools good good study day day up
    Java反射机制
    第一篇:《UNIX 环境高级编程》编译环境的搭建
  • 原文地址:https://www.cnblogs.com/study100/p/3252680.html
Copyright © 2011-2022 走看看