Jump to content

Download a TXT


b2k

Recommended Posts

Hi guys, im here again lol..

 

Ok, im development an script that has interaction with MySQL. So, its kinda a portal, but in the main index i show some articles, i want to give my visitor the opportunity to download this article...

 

See the query that i send

 

$result = mysql_query('SELECT `id`, `page`, `header`, `date`, `content`, `contador` FROM `webapp` ORDER BY `id` DESC LIMIT 6'); 

 

So, the articles content are stores in content field...How i can download the article into a txt

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/90143-download-a-txt/
Share on other sites

Retrieve the Article's data into seperate strings, and use the fopen, fwrite functions to create then insert data into a new txt file. Google for: PHP file Create, and PHP file Write.

 

Its best to do this when making a new article. Then do the method i showed you, and in a new row in your article table, make a row called download with the txt name. Then use

 

<?php echo  '<a href = "'.$row['download'].'">Download Article</a>

Link to comment
https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462251
Share on other sites

create a link that will run the same query only set the header's to a text file,

 

I don't know the setting for a text file off the top of my head,

though taking a look at http://us3.php.net/manual/en/function.header.php

 

will get you started in the right direction,

 

once you have the header set print out the text to the screen

Link to comment
https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462252
Share on other sites

maybe something like this?

 

download.php

<?php

mysql_connect('xxx','yyyy','zzz');
mysql_selectdb('abc');

if(!empty($id=(isset($_GET['id'])?intval($_GET['id']):0)))
{
    if($result = mysql_query('SELECT `id`, `content` FROM `webapp` WHERE id=$id') && $row=mysql_fetch_assoc($result))
    {
        $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');
}
           
?>

 

now all ya need is the link to this :)

<A HREF='download.php?id=1'>Article 1</A>

 

Link to comment
https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462285
Share on other sites


To get ID:

 

<?php

mysql_connect('xxx','yyyy','zzz');
mysql_selectdb('abc');

$result = mysql_query('SELECT `id`, `page`, `header`, `date`, `content`, `contador` FROM `webapp`'); 

while($row = mysql_fetch_array($result)){


echo  '<a href = "'.$row['id'].'">$row

</a>' . "<br>";


}
?>

Link to comment
https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462361
Share on other sites


Error was in: mysql_selectdb

 

<?php

mysql_connect('xxx','yyyy','zzz');
mysql_select_db('abc');

$result = mysql_query('SELECT `id`, `page`, `header`, `date`, `content`, `contador` FROM `webapp`'); 

while($row = mysql_fetch_array($result)){


echo  '<a href = "'.$row['id'].'">$row

</a>' . "<br>";


}
?>

Link to comment
https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462368
Share on other sites

Sorry but i was ponting to the other code, this is what i got so far

 

<?php
include "config.php";


if(!empty($id=(isset($_GET['id'])?intval($_GET['id']):0)))
{
$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');
}
           
?>

 

Its giving me error in line 6

Link to comment
https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462371
Share on other sites

You have a random "{" somewhere in your code, and I set this new one up for you

 

<?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');
}
           
?>

Link to comment
https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462386
Share on other sites

Now WORKK!!!!!!!

 

But the text that write into the TXT is 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

 

Weird...

Link to comment
https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462404
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.