b2k Posted February 8, 2008 Share Posted February 8, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/ Share on other sites More sharing options...
phpSensei Posted February 9, 2008 Share Posted February 9, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462251 Share on other sites More sharing options...
amites Posted February 9, 2008 Share Posted February 9, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462252 Share on other sites More sharing options...
phpSensei Posted February 9, 2008 Share Posted February 9, 2008 EDIT: <?php echo '<a href = "'.$row['download'].'">Download Article</a>'; Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462253 Share on other sites More sharing options...
laffin Posted February 9, 2008 Share Posted February 9, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462285 Share on other sites More sharing options...
b2k Posted February 9, 2008 Author Share Posted February 9, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462357 Share on other sites More sharing options...
phpSensei Posted February 9, 2008 Share Posted February 9, 2008 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462361 Share on other sites More sharing options...
b2k Posted February 9, 2008 Author Share Posted February 9, 2008 Thanks!... This what happens Parse error: parse error, unexpected '=', expecting ')' in W:\www\download.php on line 6 Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462362 Share on other sites More sharing options...
phpSensei Posted February 9, 2008 Share Posted February 9, 2008 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462368 Share on other sites More sharing options...
b2k Posted February 9, 2008 Author Share Posted February 9, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462371 Share on other sites More sharing options...
phpSensei Posted February 9, 2008 Share Posted February 9, 2008 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'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462386 Share on other sites More sharing options...
b2k Posted February 9, 2008 Author Share Posted February 9, 2008 Thanks sensei...but here what say now Parse error: parse error, unexpected ';' in W:\www\download.php on line 12 Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462393 Share on other sites More sharing options...
phpSensei Posted February 9, 2008 Share Posted February 9, 2008 Line 12 has missing a ")" use $content=$row['content'] . (strlen($row['content']) < 256 )? str_repeat('\n',256-$total):''; // Fix annoying IE bug Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462395 Share on other sites More sharing options...
b2k Posted February 9, 2008 Author Share Posted February 9, 2008 Hi, this is funny now error Parse error: parse error, unexpected ':' in W:\www\download.php on line 13 Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462400 Share on other sites More sharing options...
phpSensei Posted February 9, 2008 Share Posted February 9, 2008 Fix $total = strlen($row['content']): to $total = strlen($row['content']); Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462402 Share on other sites More sharing options...
b2k Posted February 9, 2008 Author Share Posted February 9, 2008 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... Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462404 Share on other sites More sharing options...
blueman378 Posted February 9, 2008 Share Posted February 9, 2008 i cant be bothered counting but is there 256 \n's ? if it is im gueesing it has something to do with the "annoying ie bug" line Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462407 Share on other sites More sharing options...
laffin Posted February 9, 2008 Share Posted February 9, 2008 well damn, those shudda been double instead of single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462449 Share on other sites More sharing options...
b2k Posted February 9, 2008 Author Share Posted February 9, 2008 Seems like i have to find other method Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-462578 Share on other sites More sharing options...
b2k Posted February 10, 2008 Author Share Posted February 10, 2008 any solution people?! Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-463051 Share on other sites More sharing options...
b2k Posted February 10, 2008 Author Share Posted February 10, 2008 help! Quote Link to comment https://forums.phpfreaks.com/topic/90143-download-a-txt/#findComment-463116 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.