Experience [PHP] - download the attachment

<?php
    $ File_name = "textuse.zip"; // download file name    
    $ File_dir = "E: / www / xiazaiwenjian /"; // download file storage directory    
    var_dump ($ file_name is);
    // Check if file exists    
    if (! file_exists ( $file_dir . $file_name )) {    
        header('HTTP/1.1 404 NOT FOUND');  
    } else {    
        // open the file in read-only mode and binary   
        $file = fopen ( $file_dir . $file_name, "rb" ); 
        // tells the browser which is a file stream file format    
        Header ( "Content-type: application/octet-stream" ); 
        // request metric range  
        Header ( "Accept-Ranges: bytes" );  
        // Content-Length is the byte length specified in the request or response comprising a data    
        Header ( "Accept-Length: " . filesize ( $file_dir . $file_name ) );  
        // tells the browser files as attachments that can be downloaded after downloading the file name $ file_name value of the variable.
        Header ( "Content-Disposition: attachment; filename=" . $file_name );    
        // read the contents of the file and output directly to the browser
        echo fread ( $file, filesize ( $file_dir . $file_name ) );    
        fclose ($ file); // open when you want to be close the file
        exit (); 
    }    
?>

Guess you like

Origin www.cnblogs.com/cczhao06/p/12558979.html