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
|