Php implements the instance code 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 php implementation of the current page Click to download the file instance code is all the content shared by xiaobian to everyone, I hope to give you a reference, but also hope you can support a lot of homes.