Logical1 Posted March 4, 2009 Share Posted March 4, 2009 Hello Can anyone give an example or explain how to generate a text file on the viewers' desktop (or wherever they choose) ? I am trying to give an option to copy parts of the text on my page and save it as a text file for the viewer. So to avoid a couple of clicks and copy and paste. I have found a lot of info about writing to a text file, but they all get saved on MY server, but they should be on the VIWER'S computer. Thanks in adavance. PS. The code I am using right now is pasted bellow but as I said it does not get saved for the viewer. <?php $File = date("m").".txt"; $Handle = fopen($File, 'w'); $Data = "Test data bla bla bla \n\n"; fwrite($Handle, $Data); fclose($Handle); ?> Link to comment https://forums.phpfreaks.com/topic/147873-write-to-a-text-file/ Share on other sites More sharing options...
WolfRage Posted March 4, 2009 Share Posted March 4, 2009 Create a text file make a link to it so they can download and save it. Link to comment https://forums.phpfreaks.com/topic/147873-write-to-a-text-file/#findComment-776111 Share on other sites More sharing options...
Logical1 Posted March 4, 2009 Author Share Posted March 4, 2009 The contant is different depending on what blog or message they will see. So it won't be a practical solution. I can create and fill the text file but it all is done invisibly. If I could keep the file open so the viewer can see it they can choose a location and save. Question is how to do that? Link to comment https://forums.phpfreaks.com/topic/147873-write-to-a-text-file/#findComment-776712 Share on other sites More sharing options...
WolfRage Posted March 5, 2009 Share Posted March 5, 2009 Ok look I don't understand how it is not a pratical solution, the only way you are going to get anything to the user is if they download and chose to save it, period. If you want a confirmation then create the file, add that name to the user's session then redisplay the contents, if they are satisfied give them a link to the file for downloading. Once the download is complete Unlink the file. Link to comment https://forums.phpfreaks.com/topic/147873-write-to-a-text-file/#findComment-777061 Share on other sites More sharing options...
Logical1 Posted March 6, 2009 Author Share Posted March 6, 2009 I have no problem with visitors wanting to select where to save or even if they want to save. As a matter of fact this is how I want this thing to work. The issue is that I don't want to sit ana manually make a text file for every entry in the system, and I can not think of a way to automate it. If I make a generic text file it wo't have the content for that specific entry. Or maybe you know a way to do this and I don't know about that? Supposed you were makikng a forum and wanted to give option of saving a message or reply in text format without having to opy and paste the content manually. How would you do it? Each message would be different, without having to make a separate text file for each message and updating it every time there is an answer what solution do you see? Secondly, if it is so easy to open a text file, write to it and close the file again, why is it impossible to give the option to the viewer to see it on the screen if they want to and decide if they want to save it or not? Link to comment https://forums.phpfreaks.com/topic/147873-write-to-a-text-file/#findComment-777940 Share on other sites More sharing options...
WolfRage Posted March 7, 2009 Share Posted March 7, 2009 Ok you are obviously getting frustrated, but because you want a more user friendly system, there is no simple solution, you need a multipart solution. This is how I would do it. Person A wants to save a message from your forum, they click the button that activates this feature. Your script then takes the text of the message as a variable, it appends to the begining of the string some useful information for later, like a file name and a date. You open a new file with a generated name proabbly using a combination of the topic, date, and time. Write this string to the file, save and close the file. Reopen the file and display the contents to Person A, he checks it over adds anything that he would like to, and then repost if neccessary with the modified content, you repeat the process if he repost except you open the same file and write over the existing data. We can pass the file name along in his session or via post or get. If he excepts the file contents as they are, you pass him a link for downloading the file, he does so and saves it. Your script also passes the name of the file and the time of which it was downloaded to a log file. A cron script will then check this log file and delete any of the new download files that have been generated which are older than a day or so. If you want to further make sure that two files could not be generated at the same time for the same person, then add there user name to the file name. Does this solution work for all of your needs, if not I am sure you can see where I am going and could probably customize it from here. Now that I have given you the idea, you program it, and if you run into more trouble I will help you out. But I am not going to program it for you. Link to comment https://forums.phpfreaks.com/topic/147873-write-to-a-text-file/#findComment-778977 Share on other sites More sharing options...
Logical1 Posted March 7, 2009 Author Share Posted March 7, 2009 Thanks I think I got the idea and you do not have to program it for me. I will write the script and if there were any problems post it here. Thasnk for the suggestions Link to comment https://forums.phpfreaks.com/topic/147873-write-to-a-text-file/#findComment-779135 Share on other sites More sharing options...
Logical1 Posted March 8, 2009 Author Share Posted March 8, 2009 I wrote a PHP page that picks up the data from the MySQL database and sends it to the text file. So far so good. But it is not working for me becasue let's say you are at the page OneThread.php?Id=100 in the forum and looking at the entry number 100. There is a link on the page that says "Note in text format". You click on it and it sends you to the page Generate_Text.php and that page creates a fresh copy of your text version (say Entry100.txt). You see there is a middle step and the link on the page is not actually to the text file. That's what I am trying to avoid. If I generate a text file every time the OneThread.php is viewed I can make the link on that page to Entry100.txt (for example). But this measn creating a lot of text files while in reality not many people might want to use them (by creating the every time the page is opened). What do you think? I guess subconsiously I knew it was not going to work. I Link to comment https://forums.phpfreaks.com/topic/147873-write-to-a-text-file/#findComment-779293 Share on other sites More sharing options...
WolfRage Posted March 8, 2009 Share Posted March 8, 2009 Ok so in this case you do not wnat them to preveiw the text, you just want to send them a text file. Still better to make that file on the fly, and not pregenerate. So when they click the link begin to execute your script that makes the text file, then have it pass a redirect header() to the text file, so the user can dowload it. Use the cron job to delete files older than one day, then you are only generating the files on demand. If this is not a good enough solution and you want to prebuild the files then just have the script check to see if the file already exists, if it does then do not recreate it. Link to comment https://forums.phpfreaks.com/topic/147873-write-to-a-text-file/#findComment-779298 Share on other sites More sharing options...
Logical1 Posted March 8, 2009 Author Share Posted March 8, 2009 I really don't see how your suggestion can work. As you know PHP is not like Java that clicking on a button could initiate an event and I do not want to go to a middle page (there is no need for a preview, thay are already seeing the content). Why don't you put your suggestion into action in a very simple code (Just pass 2 variables) and don't have to worry about where to get the variables. I will supply you with the basics to save time: Here is the code for the thread page: <?php $V1= "GOOD"; $V2="DAY"; print "<a href=".$Id.".txt>Right click here to save a text file version</a><br><br>"; Print "Variable one: ".$V1."<br>"; print "Variable two: ".$V2."<br>"; ?> Here is your text generator file: <?php $name = "$Id"; $file = "$name.txt"; $values = "Variable one: $V1\nVariable two: $V2\n\n" $fp = fopen($file, "w") or die("Couldn't open $file for writing!"); $numBytes = fwrite($fp, $values) or die("Couldn't write values to file!"); fclose($fp); ?> And here is what the text file will look like: Variable one: GOOD Variable two: DAY Link to comment https://forums.phpfreaks.com/topic/147873-write-to-a-text-file/#findComment-779878 Share on other sites More sharing options...
WolfRage Posted March 9, 2009 Share Posted March 9, 2009 Look I could easily get what I have stated to work, but instead I was trying to help you out. I have been programing for a long time, and so I don't appreciate the comments. If you don't want help solving your problems then do not post asking for it. Good luck in the future, but going around rejecting helpful suggestions that are trying to guide you will get you no where fast. Link to comment https://forums.phpfreaks.com/topic/147873-write-to-a-text-file/#findComment-780182 Share on other sites More sharing options...
Logical1 Posted March 9, 2009 Author Share Posted March 9, 2009 I have no idea why you are acting funny. I said that I don't see how your suggestion can come of and provided you with the code to show how it can be done. There are 2 possibilites either it works or it does't either way it takes 10 minutes to find out and no need to act offended. I have provided you with the code it takes no more than 10 minutes to show how you can do this according to your system for only one variable. Also these forums are for exchange of ideas and learning, people post questions about what they do not know and others answer nobody tries to offend or gets offended for someone saying do an example. As they say proof is in the pudding. Link to comment https://forums.phpfreaks.com/topic/147873-write-to-a-text-file/#findComment-780389 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.