- $file = ' a.pdf ';
- 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 ');
- B_clean ();
- Flush ();
- ReadFile ($file);
- Exit
- }
- ?>
Copy Code2. Output generated files (such as CSV PDF, etc.) sometimes the system that will output the generated files, mainly generate csv,pdf, or packaging multiple files for the zip format to download, for this part, some of the implementation method is to generate the output as a file and then through the file download, Finally delete the makefile, you can actually output the file by Php://output directly, the following CSV output as an example.
- Header (' Content-description:file Transfer ');
- Header (' Content-type:application/octet-stream ');
- Header (' content-disposition:attachment; Filename=a.csv ');
- Header (' content-transfer-encoding:binary ');
- Header (' expires:0 ');
- Header (' Cache-control:must-revalidate, post-check=0, pre-check=0 ');
- Header (' Pragma:public ');
- Ob_clean ();
- Flush ();
- $rowarr =array (Array (' 1 ', ' 2 ', ' 3 '), Array (' 1 ', ' 2 ', ' 3 '));
- $fp =fopen (' Php://output ', ' W ');
- foreach ($rowarr as $row) {
- Fputcsv ($fp, $row);
- }
- Fclose ($FP);
- Exit
- ?>
Copy Code3. Get the contents of the generated file, do the output after processing to get the contents of the generated file is generally Mr. Cheng file, then read, and finally delete, in fact, this can use Php://temp to do the operation, the following is still a CSV example
- Header (' Content-description:file Transfer ');
- Header (' Content-type:application/octet-stream ');
- Header (' content-disposition:attachment; Filename=a.csv ');
- Header (' content-transfer-encoding:binary ');
- Header (' expires:0 ');
- Header (' Cache-control:must-revalidate, post-check=0, pre-check=0 ');
- Header (' Pragma:public ');
- Ob_clean ();
- Flush ();
- $rowarr =array (Array (' 1 ', ' 2 ', ' Chinese '), Array (' 1 ', ' 2 ', ' 3 '));
- $fp =fopen (' php://temp ', ' r+ ');
- foreach ($rowarr as $row) {
- Fputcsv ($fp, $row);
- }
- Rewind ($FP);
- $filecontent =stream_get_contents ($FP);
- Fclose ($FP);
- Working with $filecontent content
- $filecontent =iconv (' UTF-8 ', ' GBK ', $filecontent);
- Echo $filecontent; Output
- Exit
- ?>
Copy CodePHP in the Input/output streams function is very powerful, with good, can simplify coding, improve efficiency, we recommend you specifically into the OH. |