Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Script to force download text file
    text
    copied!<p>I have a Text file created by php script using str_pad() with spaces, I want to download the same without losing those spaces. When I download using ftp, there is no issue, all the spaces in the text files are intact. But when I use this download script so that the user can download, it gives junk characters instead of spaces and data is scrambled. Pl help if there is any other way of downloading this file by the user.</p> <p>My php file to download - download.php:</p> <pre><code>&lt;?php function output_file($file, $name, $mime_type='') { if(!is_readable($file)) die('File not found or inaccessible!'); $size = filesize($file); $name = rawurldecode($name); $known_mime_types=array( "txt" =&gt; "text/plain", "html" =&gt; "text/html", "php" =&gt; "text/plain" ); if($mime_type==''){ $file_extension = strtolower(substr(strrchr($file,"."),1)); if(array_key_exists($file_extension, $known_mime_types)){ $mime_type=$known_mime_types[$file_extension]; } else { $mime_type="application/force-download"; }; }; @ob_end_clean(); if(isset($_SERVER['HTTP_RANGE'])) { list($a, $range) = explode("=",$_SERVER['HTTP_RANGE'],2); list($range) = explode(",",$range,2); list($range, $range_end) = explode("-", $range); $range=intval($range); if(!$range_end) { $range_end=$size-1; } else { $range_end=intval($range_end); } $new_length = $range_end-$range+1; header("HTTP/1.1 206 Partial Content"); header("Content-Length: $new_length"); header("Content-Range: bytes $range-$range_end/$size"); } else { $new_length=$size; header("Content-Length: ".$size); } $chunksize = 1*(1024*1024); $bytes_send = 0; if ($file = fopen($file, 'r')) { if(isset($_SERVER['HTTP_RANGE'])) fseek($file, $range); while(!feof($file) &amp;&amp; (!connection_aborted()) &amp;&amp; ($bytes_send&lt;$new_length) ) { $buffer = fread($file, $chunksize); print($buffer); //echo($buffer); flush(); $bytes_send += strlen($buffer); } fclose($file); } else die('Error - can not open file.'); die(); } set_time_limit(0); $file_path=$_REQUEST['filename']; output_file($file_path, ''.$_REQUEST['filename'].'', 'text/plain'); ?&gt; </code></pre> <p>HTML file which calls the download.php</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Downloading Text file&lt;/title&gt; &lt;/head&gt;&lt;body&gt; &lt;br&gt; &lt;br&gt; &lt;a href='download.php?filename=Text1.txt'&gt;Download print file &lt;/a&gt;&lt;br&gt; &lt;br&gt; &lt;br&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>My original Text1.txt file output which is created by a php script using str_pad():</p> <pre><code> 3M India Ltd 78788 No.2, Abbayappa Layout, 4th Main, NS Palaya, Bannerghatta Road, Bangalore 560 076 11/05/2012 10:10:57 500 322 AAACB5984DXM001 29400127541 KA-01-N-2345 29400127541 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload