Download a TXT

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
b2k
Forum Newbie
Posts: 6
Joined: Sun Feb 10, 2008 12:58 pm

Download a TXT

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Download a TXT

Post 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)
 
b2k
Forum Newbie
Posts: 6
Joined: Sun Feb 10, 2008 12:58 pm

Re: Download a TXT

Post by b2k »

Hi,

Thanks for the anwser.. Now i get this:

Image

I posted in image because i can't copy that character..
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Download a TXT

Post by Benjamin »

Windows uses "\r\n" for line feeds.
b2k
Forum Newbie
Posts: 6
Joined: Sun Feb 10, 2008 12:58 pm

Re: Download a TXT

Post by b2k »

I didn't get it
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Re: Download a TXT

Post 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.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Download a TXT

Post 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!
b2k
Forum Newbie
Posts: 6
Joined: Sun Feb 10, 2008 12:58 pm

Re: Download a TXT

Post by b2k »

Still the problem
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Download a TXT

Post 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:
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Download a TXT

Post 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());
}
 
Post Reply