Php is a simple method for clicking to download files on the current page,

Source: Internet
Author: User

Php is a simple method for clicking to download files on the current page,

Php controller code

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 '000000';} if (! File_exists ($ file_url) {// check whether the file contains echo '000000';} $ 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 the file // enter the 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); // 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 for execution.

The above is all the simple methods provided by the editor for php to download files on the current page. I hope it will be helpful to you and support more help ~

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.