This article introduces the code for reading and downloading files using php. the header function is required for php to download files. for details, refer. The code is as follows: Copy the code l...
This article introduces the code for reading and downloading files using php. the header function is required for php to download files. for details, refer.
The code is as follows: |
|
$ File = 'monkey.gif '; If (file_exists ($ file )){ Header ('content-Description: File Transfer '); Header ('content-Type: application/octet-stream '); Header ('content-Disposition: attachment; filename = '. basename ($ file )); Header ('content-Transfer-Encoding: binary '); Header ('expires: 0 '); Header ('cache-Control: must-revalidate, post-check = 0, pre-check = 0 '); Header ('pragma: public '); Header ('content-Length: '. filesize ($ file )); Ob_clean (); Flush (); Readfile ($ file ); Exit; } ?> |
The above code is the download code
Next, paste the code for previewing PDF files online.
The code is as follows: |
|
Public function fddAction () { // Get attachment location $ Attachment_location = $ _ SERVER ["DOCUMENT_ROOT"]. "/pdf/fdd/sample.pdf "; If (file_exists ($ attachment_location )){ // Attachment exists // Send open pdf dialog to user Header ('cache-Control: public '); // needed for I. e. Header ('content-Type: application/pdf '); Header ('content-Disposition: inline; filename = "sample.pdf "'); Readfile ($ attachment_location ); Die (); // stop execution of further script because we are only outputting the pdf } Else { Die ('Error: File not found .'); } } ?> |
Address:
Reprinted at will, but please attach the article address :-)