Page 1 of 1

Download a TXT

Posted: Sun Feb 10, 2008 1:01 pm
by b2k
Hi everybody,

Im been triying to create a function/code that help me to download an article from my database into a txt... This what i got so far..

Code: Select all

<?php
include "config.php";
 
$id  =      (isset($_GET['id']))     ?     intval($_GET['id']) :   0;
 
if(!empty($id)){
$result = mysql_query('SELECT `id`, `page`, `header`, `date`, `content`, `contador` FROM `webapp`');    
        $filename = "$id.txt";
        // Push headers that tell what kind of file is coming down the pike
        header('Content-type: text/plain');
        header('Content-Disposition: attachment; filename='.$filename);
 $content=$row['content'] . (strlen($row['content']) < 256 )? str_repeat('\n',256-$total):''; // Fix annoying IE bug
 $total     = strlen($row['content']);
        header('Content-length: '.$total);
        
        echo $content;
               
} else {
    die('Unknown File');
}
           
?>
The download its fine but when i open the file i get this:
\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
Why this happens?

Thanks

Re: Download a TXT

Posted: Sun Feb 10, 2008 1:04 pm
by Benjamin
Well if you want 256 new lines

Code: Select all

 
str_repeat('\n',256-$total)
 
should be

Code: Select all

 
str_repeat("\n",256-$total)
 

Re: Download a TXT

Posted: Sun Feb 10, 2008 1:11 pm
by b2k
Hi,

Thanks for the anwser.. Now i get this:

Image

I posted in image because i can't copy that character..

Re: Download a TXT

Posted: Sun Feb 10, 2008 1:39 pm
by Benjamin
Windows uses "\r\n" for line feeds.

Re: Download a TXT

Posted: Sun Feb 10, 2008 8:03 pm
by b2k
I didn't get it

Re: Download a TXT

Posted: Mon Feb 11, 2008 4:13 am
by Rovas
It' s a problem with the character encoding in your database and the one that Notepad uses. For example you may use utf-8 in your database and the Notepad uses ANSI. Use the rtf or html format.
You have to use beside Line feed \n Carriege Return i.e. \r and because of the mode that Microsoft set new lines that was astions referring to.

Re: Download a TXT

Posted: Mon Feb 11, 2008 7:27 am
by andym01480
Guys you are missing something fairly fundamental!

Code: Select all

<?php
include "config.php";
 
$id  =      (isset($_GET['id']))     ?     intval($_GET['id']) :   0;
 
if(!empty($id)){
$result = mysql_query('SELECT `id`, `page`, `header`, `date`, `content`, `contador` FROM `webapp`');    
        $filename = "$id.txt";
        // Push headers that tell what kind of file is coming down the pike
        header('Content-type: text/plain');
        header('Content-Disposition: attachment; filename='.$filename);
 $content=$row['content'] . (strlen($row['content']) < 256 )? str_repeat('\n',256-$total):''; // Fix annoying IE bug
 $total     = strlen($row['content']);
        header('Content-length: '.$total);
        
        echo $content;
               
} else {
    die('Unknown File');
}
           
?>
How are you getting from mysql $result to the $row array? That would be a major reason why there is nothing other than the new line problems every one else has pointed out!

Re: Download a TXT

Posted: Mon Feb 11, 2008 7:59 pm
by b2k
Still the problem

Re: Download a TXT

Posted: Tue Feb 12, 2008 4:23 am
by andym01480
Still what problem - too many newlines or nothing between the new lines?

Have you changed the code to fill $row array? If so post it so we can help you!!! Hint mysql_fetch_assoc() is quite good at doing that when combined with a while loop. :wink:

Re: Download a TXT

Posted: Wed Feb 13, 2008 1:46 pm
by yacahuma
try this. I found it on php.net(i think)

Code: Select all

 
function send_file($name) {
  ob_end_clean();
  $path = "/home/webuser/webapache/uploads/".$name;
  if (!is_file($path) or connection_status()!=0) return(FALSE);
  header("Cache-Control: no-store, no-cache, must-revalidate");
  header("Cache-Control: post-check=0, pre-check=0", false);
  header("Pragma: no-cache");
  header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
  header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
  header("Content-Type: application/octet-stream");
  header("Content-Length: ".(string)(filesize($path)));
  header("Content-Disposition: inline; filename=$name");
  header("Content-Transfer-Encoding: binary\n");
  if ($file = fopen($path, 'rb')) {
    while(!feof($file) and (connection_status()==0)) {
      print(fread($file, 1024*8));
      flush();
    }
    fclose($file);
  }
  return((connection_status()==0) and !connection_aborted());
}