How to force files of type . txt to be downloaded

Asked

Viewed 9,163 times

13

I have a page, but I don’t want you to click on files like . txt to download instead of displaying them. I am developing this page in PHP.

I saw some scripts on the web, but it is forcing the download of files of type . txt without it being at least clicked.

Follow the code I’m making:

<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>

2 answers

12

To force a txt file download 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();
}

Use of the Code:

download('forca_download.txt');

References:

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

9


According to this post , you can do this by changing the file .htaccess 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 headers:

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

According to this response from the SOEN, to use the htaccess header setting feature, you must activate an Apache module.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.