How to force .txt files to be downloaded [duplicate]

12

I have a page, but I do not want it when I click on files of type .txt to download, instead of displaying them. I'm developing this page in PHP.

I have seen some script on the WEB, however it is forcing the download of .txt files without it being at least clicked on.

Here is the code I'm doing:

<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9"/>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
    <title>FTP</title>
    <link rel="stylesheet" href="css/style.css" />
</head>
    <body>
        <?php
        if(isset($_GET['path']))
            $dir =  $_GET['path'];
        else
            $dir = '..'. DIRECTORY_SEPARATOR .'ftp';
        foreach (new DirectoryIterator($dir) as $file) {
            if (!$file->isDot()) {
                if ($file->isDir()) {
                    echo '<div class="pastas">';
                        echo '<a href="index.php?path='.$dir. DIRECTORY_SEPARATOR .$file.'"><img src="icones/archive.ico" width="30px" height="30px"/></a>' . $file->getBaseName();
                    echo '</div>';
                } else {
                    switch($file->getExtension()) {
                        case 'txt':
                        echo '<a href=""><img src="icones/text.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                        break;
                        case 'jpg':
                        echo '<a href=""><img src="icones/jpg.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                        break;
                        case 'gif':
                        echo '<a href=""><img src="icones/jpg.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                        break;
                        case 'docx':
                        echo '<a href=""><img src="icones/doc.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                        break;
                        case 'doc':
                        echo '<a href=""><img src="icones/doc.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                        break;
                        case 'pdf':
                        echo '<a href=""><img src="icones/pdf.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                        break;
                        case 'ppt':
                        echo '<a href=""><img src="icones/ppt.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                        break;
                        case 'rar':
                        echo '<a href=""><img src="icones/rar.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                        break;
                        case 'zip':
                        echo '<a href=""><img src="icones/zip.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                        break;
                        case 'avi':
                        echo '<a href=""><img src="avi.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                        break;
                        case 'mp3':
                        echo '<a href=""><img src="icones/mp3.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                        break;
                        case 'wmv':
                        echo '<a href=""><img src="icones/wmv.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                        break;
                        case 'exe':
                        echo '<a href=""><img src="icones/exe.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                        break;
                        default:
                        echo '<a href=""><img src="icones/default.ico" width="30px" height="30px" /></a>'.$file->getBaseName();
                    }
                }
            }
        }
?>
    </body>

    
asked by anonymous 24.04.2014 / 14:17

2 answers

9

Second this post , you can do this by changing the .htaccess file with the following:

<FilesMatch "\.(?i:txt)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

This causes all text files to be served with the headers:

Content-Type: application/octet-stream
Content-Disposition: attachment;

According to this SOEN response , to use the htaccess header definition feature, it is necessary to activate a module from the Apache.

    
24.04.2014 / 14:24
10

To force a download of txt files I would use the function just below:

Code:

function download($arquivo){
      header("Content-Type: application/force-download");
      header("Content-Type: application/octet-stream;");
      header("Content-Length:".filesize($arquivo));
      header("Content-disposition: attachment; filename=".$arquivo);
      header("Pragma: no-cache");
      header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
      header("Expires: 0");
      readfile($arquivo);
      flush();
}

Code Usage:

download('forca_download.txt');

References:

Obs: It would be a way to not configure anything on the server transparently, and that works on all servers, which often does not let you change the settings.

    
24.04.2014 / 15:48