Jump to content

[SOLVED] Creating Text Files on the fly


jyushinx

Recommended Posts

Hi,

 

What I want to do is allow a logged in user to download a text file version of the list they are viewing on the screen. So if they are viewing the Paid list of users, there should be a button on that same page that says Download To Text File, and when clicked, the user is prompted to download a text file version of the list.

 

What is the best practice to go about doing this?

 

Thanks.

Link to comment
Share on other sites

I would send them to a PHP file like this (using a form on the page with the list):

 

<?php
$filename = "whatever.txt";
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="'.$filename.'"');
echo 'Your list here. Either echoed the same way as you echoed the list on previous page, or echoed from hidden text fields in the form, containing the list.';
?>

 

That way they are prompted to open/download the .txt file, and you can call it whatever you want.

Link to comment
Share on other sites

thebadbad,

 

That is exactly what I am looking for. I have one more question however.

 

When I display the list as a hidden input variable, I base64_encode it. The problem is that when I decode it on the other end, I lose the "\n" new line characters. They're replaced with the unknown character symbol. Any ideas how to preserve this?

 

Thanks.

Link to comment
Share on other sites

Can you show the code where you encode the text and echo it in the hidden field?

 

EDIT-

I made some tests, and I don't seem to loose line breaks when I base64. This is the test I did:

 

<?php
if(!isset($_POST['submit']))
{
$var = <<<HEREDOC
bla
bla
bla
HEREDOC;

echo "<form method=POST>
<input type=\"hidden\" name=\"0\" value=\"".base64_encode($var)."\">
<input type=\"submit\" name=\"submit\">";
die;
}

echo "<pre>".base64_decode($_POST['0'])."</pre>";


?>

 

 

Orio.

Link to comment
Share on other sites

The code on the form page is:

 

<form name="generateTextForm" action="generatetext.php" method="post">
<input type="hidden" name="textFileName" value="someFileName.txt"><input type="hidden" name="textFileContent" value="<?PHP print base64_encode($textFileContent); ?>">
</form>

 

The code on the receiving script:

 

$textFileContent = ( isset($_POST['textFileContent']) ? $_POST['textFileContent'] : "");
$filename = ( isset($_POST['textFileName']) ? $_POST['textFileName'] : "");

$textFileContent = base64_decode($textFileContent);

if( empty($textFileContent) || empty($filename) )
{
exit(0);	
}
else
{
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="'.$filename.'"');
print $textFileContent;
}

 

When the submit button is clicked on the form page, I am prompted to download a text file, but the the text file reads:

 

Text File Title[][]John Smith[][]Jane Smith

 

The [] is my representation of what looks like a tiny rectangle character. Now when I run this same script without the base64 encoding, the text file has line breaks where those rectangle characters are.

 

Any ideas?

Link to comment
Share on other sites

I've tested the following script on my server and I got perfect results- with line breaks. Try it yourself:

 

<?php

$self = basename($_SERVER['PHP_SELF']);

if(!isset($_POST['submit']))
{

$textFileContent = file_get_contents($self);
?>
<form name="generateTextForm" action="<?php echo $self; ?>" method="post">
<input type="hidden" name="textFileName" value="<?php echo $self; ?>"><input type="hidden" name="textFileContent" value="<?PHP print base64_encode($textFileContent); ?>">
<input type="submit" name="submit">
</form>
<?php
die;
}


$textFileContent = ( isset($_POST['textFileContent']) ? $_POST['textFileContent'] : "");
$filename = ( isset($_POST['textFileName']) ? $_POST['textFileName'] : "");

$textFileContent = base64_decode($textFileContent);

if( empty($textFileContent) || empty($filename) )
{
exit(0);	
}
else
{
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="'.$filename.'"');
print $textFileContent;
}

?>

 

 

Orio.

Link to comment
Share on other sites

Ok, this thing is killing me. I cannot figure out for the life of me why my script won't work.

 

A point of interest: I was trying to base64_encode the string and then submit to my generatetext script where I would decode and send the file. This was causing the newlines to disappear. However, for the sake of experimentation, I tried not encoding on the submitting page but simply encoded and immediately decoded on the receiving script, it worked fine. So the base64_encode is not the issue. Somehow or another, during the POST process this is getting messed up if it is encoded. Remember, it works fine if it isn't.

 

To give you a better idea of what I am encoding, let me explain a bit. The user is viewing a Paid list. At the same time I generate the HTML for the paid users, I generate the content for the text file. For example:

 

foreach($userList as $user)
{
  //Create HTML
  Blah Blah Blah

  //Create text file contents
    $textFileContents .= $user["Name"].", ".$user["AmountPaid"]."\n";

}

 

I then base64_encode $textFileContents and place it in a hidden value on the form which is posted to my generatetext script, decoded, and sent back as a .txt file.

 

Any more ideas?

Link to comment
Share on other sites

Try:

$textFileContents .= $user["Name"].", ".$user["AmountPaid"]."\r\n";

 

This might be necessary if you are working windows (on your pc, not on the server, but if the server is windows too that might be it too).

Anyway, try it out.

 

Orio.

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.