jyushinx Posted November 3, 2007 Share Posted November 3, 2007 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. Quote Link to comment Share on other sites More sharing options...
lighton Posted November 3, 2007 Share Posted November 3, 2007 look up fopen(to create the file) and fwrite(to write the contents) in the php manual Quote Link to comment Share on other sites More sharing options...
thebadbad Posted November 3, 2007 Share Posted November 3, 2007 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. Quote Link to comment Share on other sites More sharing options...
jyushinx Posted November 3, 2007 Author Share Posted November 3, 2007 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. Quote Link to comment Share on other sites More sharing options...
Orio Posted November 3, 2007 Share Posted November 3, 2007 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. Quote Link to comment Share on other sites More sharing options...
jyushinx Posted November 3, 2007 Author Share Posted November 3, 2007 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? Quote Link to comment Share on other sites More sharing options...
jyushinx Posted November 5, 2007 Author Share Posted November 5, 2007 Any ideas? Quote Link to comment Share on other sites More sharing options...
Orio Posted November 5, 2007 Share Posted November 5, 2007 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. Quote Link to comment Share on other sites More sharing options...
jyushinx Posted November 8, 2007 Author Share Posted November 8, 2007 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? Quote Link to comment Share on other sites More sharing options...
Orio Posted November 8, 2007 Share Posted November 8, 2007 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. Quote Link to comment Share on other sites More sharing options...
jyushinx Posted November 10, 2007 Author Share Posted November 10, 2007 Wow. That worked. The funny thing is this is on a Linux server. Whatever works though. Thanks for your help. Quote Link to comment 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.