Implement file download with PHP

xiaoxiao2021-03-05  26

If it is just a normal file download, there is no need to use PHP, with a , but sometimes Confidential, only to some people download, obviously not to tell others about the link, if so, the effect is not confidential. Below this function is a function written with PHP, which is read in a while and then transmits it to the client.

Function Download ($ FILE_DIR, $ FILE_NAME) // Parameter Description: // File_Dir: File Where / / File_name: File Name {$ file_dir = chop ($ file_dir); // Remove the extra space in the path / / Download file path if ($ file_dir! = '') {$ File_path = $ file_dir; if ($ file_dir, strlen ($ file_dir) -1, strlen ($ file_dir))! = '/') $ File_path . = '/'; $ File_path. = $ File_name;} else $ file_path = $ file_name; // Determine if the file you want to download is IF (! File_exists ($ file_path)) {echo 'sorry, the file you want to download is not exist. Return False;}

$ file_size = filesize; "Content-Type: Application / OcTet-Stream"); ​​Header ("Accept-Ranges: Bytes"); Header ("Accept-length: $ file_size); Header (" Content-disposition: attachment; filename = ". $ File_name); $ fp = fopen ($ file_path," r "); $ buffer_size = 1024; $ cur_pos = 0; while (! Feof ($ fp) && $ file_size- $ cur_pos> $ buffer_size) {$ buffer = fread ($ fp, $ buffer_size); echo $ buffer; $ cur_pos = $ buffer_size;} $ buffer = fread ($ fp, $ file_size- $ cur_pos); echo $ buffer; fclose ($ fp); return true;

}

转载请注明原文地址:https://www.9cbs.com/read-32281.html

New Post(0)