PHP implementation of the current page click the download file instance code _php instance

Source: Internet
Author: User
Tags trim

Code in the PHP controller

Public Function downfile ($path = ') {
    if (! $path) Header ("Location:/");
    Download ($path);
  

Download File Download function code

function Download ($file _url, $new _name= ') { 
    if (!isset ($file _url) | | Trim ($file _url) = = ') { 
      echo '; 
    } 
    if (!file_exists ($file _url)) {//Check the file for the presence of 
      echo ' 404 '; 
    } 
    $file _name=basename ($file _url); 
    $file _type=explode ('. ', $file _url); 
    $file _type= $file _type[count ($file _type)-1]; 
    $file _name=trim ($new _name== ") $file _name:urlencode ($new _name); 
    $file _type=fopen ($file _url, ' R '); Open file 
    //input file tag 
    header ("Content-type:application/octet-stream"); 
    Header ("Accept-ranges:bytes"); 
    Header ("Accept-length:" FileSize ($file _url)); 
    Header ("content-disposition:attachment; Filename= ". $file _name); 
    Output file content 
    echo fread ($file _type,filesize ($file _url)); 
    Fclose ($file _type);

HTML code

<iframe id= "iframe" src= "style=" display:none; ></iframe>
$ (function () {
      $ ('. DownLoad '). Click (function () {
        var path = $ (this). attr (' path ');
        $ (' #iframe '). attr (' src ', "php file path? path=" +path)
    })

Put the above code in your program, and you can do it.

The above PHP implementation of the current page click to download the file instance code is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.