Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 23rd, 2005, 09:46 AM
Registered User
 
Join Date: Dec 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default make file from textarea and download

I was trynig to make a text-html file from a posted data of a <textarea>, which had a javascript code on it. I needed to put special chars in the file in order to make the file clearer to understand, so I used the utf8_encode() function before writing. But when I try to put this file for dowload, it doesn't come with the correct format as the file in the server, it doesn't understand the "\n" char. Please check my code to see if anyone of you can help me, I really would appreciate... pleeeaaasseee....

<?php
$filepath = "download/tmp.txt";
$handler = fopen($filepath, 'a');
ftruncate($handler, '0');
//set the file size to 0.

$numpos = $_POST['numpos'];
//this is the number of textareas from which i
//will have to retrieve the information


$allowed = Array("'" , '"');
$forbidden = Array("\"", "\'");
//here I trade the magic_quotes from php to normal quotes.

for ($i=0;$i<$numpos;$i++) {
        fwrite($handler, utf8_encode(str_replace($forbidden , $allowed , "\n".$_POST["p"."$i"]."\n")));

//e.g <textarea name=p0 ; p1 ; p2 ..........
}

$contentlength = filesize($handler);
$filename = $filepath;
fclose($handler);

//this will be the complicated part.........
header("Content-Type: application/octet-stream");
header("Content-Length: ".$contentlength);
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Disposition: attachment; filename=" . $url . ".txt");
header("Content-Transfer-Encoding: text-plain");
readfile($filepath);

$fp = @fopen($filepath, "rb");
$txt = @fread($fp, filesize(basename($filepath)));
fpassthru($fp);
fclose($fp);
exit($txt);
?>

Thanks people






Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I make a Help File? chobo2 C# 2005 1 November 13th, 2007 05:36 AM
TEXTarea to text file Apocolypse2005 Javascript 1 January 19th, 2007 05:52 AM
make exe file khandangoll VB Databases Basics 2 October 9th, 2006 01:05 PM
Smaller download/single download file available jminatel BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 November 21st, 2005 11:10 AM
display vbCrLf's from textarea to textarea mat41 Classic ASP Basics 8 June 10th, 2004 12:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.