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
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
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
Share on other sites

The code that laffin give me its perfect...

 

But the problem its that when i put

 

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

 

I need to know the article ID, thats something that i don't know, i have to figure out a function to get it

Link to comment
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.