Type Here to Get Search Results !

Lesson 16: Writing to a text file

In the previous lesson, we learned to read from a text file. In this lesson, we will learn to write to a text file.
The two methods are very similar, but there is one very important difference: You must have write permissions to the file. This means that the file will have to be located in a folder where you have the necessary permissions.

If you work locally on your own computer, you can set the permissions yourself: right-click on the folder and choose "Properties". With most web hosts, you will normally have one folder with write permissions. It's often called something like "cgi-bin", "log", "databases" or something similar. If your web host permits it, you might also be able to set permissions yourself. Usually you can simply right-click on a folder in your FTP client and choose "properties" or "permissions" or something similar. The screendumps below shows how it's done in FileZilla.

FileZilla

Read more on your web host's support pages.
Note that it is the text file that needs to be in the folder with write permissions - not the PHP file.

Open the text file for writing

In the same way as when reading from a text file, the documentationfopen function is used for writing, but this time we set the mode to "w" (writing) or "a" (appending).

The difference between writing and appending is where the 'cursor' is located - either at the beginning or at the end of the text file.

The examples in this lesson use an empty text file called textfile.txt. But you can also create your own text file if you like.

First, let us try to open the text file for writing:



Example 1: Write a line to the text file

To write a line, we must use the function documentationfwrite, like this:


show example 

Since we opened the file for writing, the line is added at the top, and thus overwrites the existing line. If we instead open the file appending, the line is added at the bottom of the text file, which then will increase by one line each time it's written to.

Example 2: Adding a text block to a text file

Of course, it is also possible to add an entire text block, instead of just a single line, like this:

 <html>
 <head>
 <title>Write to a text file</title>
 </head>
 <body>

 <h1>Adding a text block to a text file:</h1>
 <form action="myfile.php" method='post'>
 <textarea name='textblock'></textarea>
 <input type='submit' value='Add text'>
 </form>

 <?php

 // Open the text file
 $f = fopen("textfile.txt", "w");

 // Write text
 fwrite($f, $_POST["textblock"]); 

 // Close the text file
 fclose($f);

 // Open file for reading, and read the line
 $f = fopen("textfile.txt", "r");

 // Read text
 echo fgets($f); 
 fclose($f);

 ?>
 
 </body>

 </html>
 
 

show example

In the next lessons, we look at another way of storing data: databases.

Post a Comment

82 Comments
  1. ambien order online no prescription common side effects ambien cr - ambien cr drug interactions

    ReplyDelete
  2. buy xanax online drug interactions soma xanax - alprazolam 0.5mg high

    ReplyDelete
  3. buy zolpidem online zolpidem tartrate 10 mg high - zolpidem er overdose

    ReplyDelete
  4. cheap alprazolam alprazolam xr 0.5mg tablets - xanax and alcohol brain damage

    ReplyDelete
  5. diazepam 5mg diazepam labesfal 10 mg - cheap diazepam pakistan

    ReplyDelete
  6. ativan without prescription addiction of ativan - treatment of ativan overdose

    ReplyDelete
  7. xanax online xanax detox - xanax withdrawal natural remedies

    ReplyDelete
  8. ativan pharmacy buy lorazepam online without rx - negative side effects ativan

    ReplyDelete
  9. online xanax white oval 2mg xanax - buy xanax valium online

    ReplyDelete
  10. cheap generic valium buy roche valium online uk - valium euphoria

    ReplyDelete
  11. where can i buy ambien online ambien medication dosage - ambien cr lethal dosage

    ReplyDelete
  12. cheap valium online no prescription valium generic trade name - order valium online mastercard

    ReplyDelete
  13. buy soma somanabolic muscle maximizer complaints - soma 5 panel drug screen

    ReplyDelete
  14. 1k4a5z4X6X vigrx plus tablets , [URL=http://vigrxplusuk.lo.gs/]continued[/URL] 0Y5t9s3f3f

    ReplyDelete
  15. 4d4n5f0e3q blog , [URL=http://vigrxplusaustralia.lo.gs/]sites[/URL] 8s6o1o6d3r

    ReplyDelete
  16. 6o2w4j3o6q vigrx plus instructions , [URL=http://vigrxplusindia.ek.la/]read full article[/URL] 4h0s0u3e6q

    ReplyDelete
  17. 1m1n1X4e0c vigrx plus ksa , [URL=http://buyvigrxplusphilippines.lo.gs/]vigrx plus ksa[/URL] 3n2w5Y2s5e

    ReplyDelete
  18. I do cоnsideг all thе conceptѕ уou've presented for your post. They are very convincing and can definitely work. Still, the posts are too quick for beginners. May just you please lengthen them a little from subsequent time? Thanks for the post.

    Here is my web site ... payday loans online
    Also visit my web site - Online Payday oan

    ReplyDelete
  19. Blogger: Network - Post a Comment ativan without rx - order lorazepam online http://www.ativanonlineoffer.com/#order-lorazepam-online

    ReplyDelete
  20. Blogger: Network - Post a Comment sibutramine price - meridia for sale http://www.meridiaordernow.com/#meridia-for-sale

    ReplyDelete
  21. Blogger: Network - Post a Comment cheap acomplia diet pills - acomplia pills http://www.a6soft.com/#acomplia-pills

    ReplyDelete
  22. Blogger: Network - Post a Comment lorazepam no prescription - buy lorazepam online no prescription http://www.ativanonlineoffer.com/#buy-lorazepam-online-no-prescription

    ReplyDelete
  23. Blogger: Network - Post a Comment reductil online - meridia price http://www.meridiaordernow.com/#meridia-price

    ReplyDelete
  24. Drugs Increase Breastmilk Production buy cheap prednisone - buy generic prednisone http://www.prednisone4sale.com/#buy-generic-prednisone

    ReplyDelete
  25. Hello, buy isotretinoin online no prescription - isotretinoin for sale http://www.josoclasolucio.com/#isotretinoin-no-prescription

    ReplyDelete
  26. Dog Ring Worm Medicine acomplia online - generic acomplia http://www.buydiscountedisotretinoin.net/#generic-acomplia

    ReplyDelete
  27. Hello, isotretinoin online - isotretinoin cost http://www.josoclasolucio.com/#cheap-accutane-no-prescription

    ReplyDelete
  28. Hello, cheap finasteride 5mg - cheap generic propecia http://www.bigdocpoker.com/#cheap-propecia-no-prescription

    ReplyDelete
  29. Hello, buy atorvastatin - lipitor 20 mg http://www.costoflipitoronline.net/#lipitor-20-mg

    ReplyDelete
  30. advitleva premarin cost - premarin drug http://www.premarindiscount.com/

    ReplyDelete
  31. Hello, cytotec online no prescription - cytotec for sale online http://www.cytotecforlessonline.net/#cytotec-for-sale-online

    ReplyDelete
  32. Hello, cheap cytotec - cytotec 200mg http://www.cytotecforlessonline.net/#cytotec-200mg

    ReplyDelete
  33. My Bodies Own Medicine topamax sale - generic topamax cost http://www.topamaxdiscount.com/

    ReplyDelete
  34. Drug Rehab Facilities In Sunnyvale generic topiramate - cheap topiramate http://www.topamaxforless.com/

    ReplyDelete
  35. dalsCleax generic accutane online - buy generic accutane http://www.isotretinoinlowcost.com/#buy-generic-accutane

    ReplyDelete
  36. Diet Drug Law Suit olanzapine without prescription - buy zyprexa http://www.perfectshutdown.com/, [url=http://www.perfectshutdown.com/]buy zyprexa online [/url]

    ReplyDelete
  37. Drug Interatctions Of Metronidazole Necon cheap meridia - meridia no prescription http://www.meridiaonlineorder.net/#meridia-no-prescription , [url=http://www.meridiaonlineorder.net/#buy-sibutramine-without-prescription ]buy sibutramine without prescription [/url]

    ReplyDelete
  38. Dicrimination And Prescribed Drug Screening buy meridia diet pills - meridia weight loss http://www.meridiaonlineorder.net/#meridia-weight-loss , [url=http://www.meridiaonlineorder.net/#meridia-sale ]meridia sale [/url]

    ReplyDelete
  39. Google Liver Medicines Exotic reductil for sale - buy reductil online no prescription http://www.meridiaonlineorder.net/#buy-reductil-online-no-prescription , [url=http://www.meridiaonlineorder.net/#meridia-diet-pills ]meridia diet pills [/url]

    ReplyDelete
  40. 2, ambien for sale - order ambien online http://www.opensourcewcms.com/#order-ambien-online , [url=http://www.opensourcewcms.com/#ambien-online-no-prescription ]ambien online no prescription [/url]

    ReplyDelete
  41. 2, finasteride online no prescription - finasteride without rx http://www.propeciagenericforsale.com/#buy-propecia-online-no-prescription , [url=http://www.propeciagenericforsale.com/#generic-propecia-online ]propecia 5mg [/url]

    ReplyDelete
  42. 2, buy finasteride online - finasteride online http://www.wheretobuymedsonline.com/propecia/], [url=http://www.wheretobuymedsonline.com/propecia/]buy finasteride [/url]

    ReplyDelete
  43. tfv [url=http://www.lamisilonlinerx.net/#terbinafine-without-prescription ]buy lamisil tablets without prescription [/url] - lamisil without rx - purchase lamisil http://www.lamisilonlinerx.net/#generic-lamisil-no-prescription ,

    ReplyDelete
  44. tfv purchase azithromycin online - generic zithromax cost http://www.zithromaxonlinerx.net/#order-zithromax, [url=http://www.zithromaxonlinerx.net/#order-zithromax]buy cheap azithromycin [/url]

    ReplyDelete
  45. tmd discount maxalt - maxalt 10 mg http://www.maxaltonlinesale.net/#maxalt-pills, [url=http://www.maxaltonlinesale.net/#maxalt-pills]generic maxalt [/url]

    ReplyDelete
  46. rvb ciprofloxacin price - cipro no prescription http://www.ciproapprovedonline.net/#cipro-no-prescription , [url=http://www.ciproapprovedonline.net/#order-cipro-online ]order cipro online [/url]

    ReplyDelete
  47. xrh generic for strattera - generic for strattera medication http://www.stratteraforsale.net/#generic-for-strattera-medication , [url=http://www.stratteraforsale.net/#buy-atomoxetine-online ]buy atomoxetine online [/url]

    ReplyDelete
  48. Hello, buy sumatriptan - buy sumatriptan online http://www.buyimitrex365.com/#buy-sumatriptan-online , [url=http://www.buyimitrex365.com/#imitrex-for-sale ]imitrex for sale [/url]

    ReplyDelete
  49. x90, sertraline online no prescription - zoloft cost http://www.zoloftgenericsale.net/ - [url=http://www.zoloftgenericsale.net/]cheap zoloft [/url]

    ReplyDelete
  50. 6, imitrex sale online - buy imitrex online http://www.imitrexonlinebuy.com/#imitrex-for-sale, [url=http://www.imitrexonlinebuy.com/#sumatriptan-without-prescription]imitrex sale[/url]

    ReplyDelete
  51. 6, imitrex sale online - buy cheap imitrex http://www.imitrexonlinebuy.com/#sumatriptan-without-prescription, [url=http://www.imitrexonlinebuy.com/#imitrex-for-sale]imitrex for sale[/url]

    ReplyDelete
  52. 6, order klonopin - klonopin online no prescription http://www.klonopinonlinediscount.com/#buy-cheap-klonopin, [url=http://www.klonopinonlinediscount.com/#klonopin-generic]buy cheap klonopin[/url]

    ReplyDelete
  53. 6, generic klonopin online - cheap klonopin online http://www.klonopinonlinediscount.com/#order-klonopin-online, [url=http://www.klonopinonlinediscount.com/#order-klonopin-online]klonopin without prescription[/url]

    ReplyDelete
  54. ooo!!! Order Topamax - buy topiramate http://www.topamaxbestonline.net/#generic-topiramate, [url=http://www.topamaxbestonline.net/#generic-topiramate]Topamax Cost[/url]

    ReplyDelete
  55. 2013 cheap bupropion - zyban quit smoking http://www.wellbutrinforsaleonline.net/, [url=http://www.wellbutrinforsaleonline.net/]zyban smoking [/url]

    ReplyDelete
  56. 2013 discount prednisone - generic prednisone http://www.prednisoneonlinerx.net/index.html, [url=http://www.prednisoneonlinerx.net/index.html]generic prednisone online [/url]

    ReplyDelete
  57. 2013 Imitrex No Prescription - buy sumatriptan online http://www.cheapimitrexbuy.net/#imitrex-price, [url=http://www.cheapimitrexbuy.net/#imitrex-no-prescription]Cheap Imitrex[/url]

    ReplyDelete
  58. 2013 cheap wellbutrin - zyban stop smoking http://www.wellbutrinforsaleonline.net/#wellbutrin-online, [url=http://www.wellbutrinforsaleonline.net/#cheap-wellbutrin]zyban sale[/url]

    ReplyDelete
  59. 2, reductil sale - sibutramine online http://www.meridiaforyourdiet.net/#reductil-sale, [url=http://www.meridiaforyourdiet.net/#buy-meridia]generic meridia[/url]

    ReplyDelete
  60. ny, Help With Assignment - http://www.onlinewritinghelp101.com/#help-with-assignment, [url=http://www.onlinewritinghelp101.com/#buy-assignments]Assignments Online[/url]

    ReplyDelete
  61. ny, levitra 10mg - cheap levitra pills http://www.recoverinchrist.org/#cheap-levitra-pills , [url=http://www.recoverinchrist.org/#levitra-online-pharmacy ]levitra online pharmacy [/url]

    ReplyDelete
  62. Hi, meridia pills for sale - order sibutramine online http://freedishprogram.com/#order-meridia, [url=http://freedishprogram.com/#meridia-online]sibutramine without prescription [/url]

    ReplyDelete
  63. 4, buy generic strattera - generic strattera online http://www.stratterafastonline.net/, [url=http://www.stratterafastonline.net/]order atomoxetine online [/url]

    ReplyDelete
  64. 4, cheap modafinil - order modafinil http://www.energyofprovigil.net/#buy-provigil, [url=http://www.energyofprovigil.net/#cheap-modafinil]buy provigil[/url]

    ReplyDelete
  65. 4, buy modafinil without prescription - provigil without prescription http://www.provigilonlineoffer.net/, [url=http://www.provigilonlineoffer.net/]buy modafinil online without prescription [/url]

    ReplyDelete
  66. 4, buy strattera - buy strattera http://www.stratterabenefits.net/#buy-strattera, [url=http://www.stratterabenefits.net/#order-strattera]generic strattera[/url]

    ReplyDelete
  67. 2, [url=http://www.cymbaltarxonline.net/]Buy Cymbalta[/url] - Cost of Cymbalta - purchase cymbalta no prescription http://www.cymbaltarxonline.net/.

    ReplyDelete
  68. Li, generic isotretinoin - buy accutane without prescription http://www.isotretinoinrxonline.net/, [url=http://www.isotretinoinrxonline.net/]generic isotretinoin[/url]

    ReplyDelete
  69. Hi, order ambien - cheap ambien online http://www.rdsns.com/, [url=http://www.rdsns.com/]order zolpidem[/url]

    ReplyDelete
  70. order imitrex online - sumatriptan online no prescription http://www.imitrexbenefits.net/, [url=http://www.imitrexbenefits.net/]generic sumatriptan[/url]

    ReplyDelete
  71. imitrex cost - buy sumatriptan no prescription http://www.imitrexbenefits.net/, [url=http://www.imitrexbenefits.net/]order imitrex online[/url]

    ReplyDelete
  72. cheap celebrex - buy celebrex without prescription http://www.celebrexonlinesale.net/, [url=http://www.celebrexonlinesale.net/]celebrex no prescription[/url]

    ReplyDelete
  73. 2, [url=http://www.dreamcoatsoft.com/]Cheap Prednisone[/url] - Generic Prednisone - order prednisolone http://www.dreamcoatsoft.com/ .

    ReplyDelete
  74. 6, [url=http://www.isotretinoinonlinerx.net/]Order Isotretinoin Online [/url] - Accutane Price - generic isotretinoin http://www.isotretinoinonlinerx.net/ .

    ReplyDelete
  75. 5, [url=http://www.costofeffexor.net/]Buy Effexor[/url] - Buy Effexor - buy venlafaxine online http://www.costofeffexor.net/ .

    ReplyDelete
  76. 12, buy clonazepam online - buy clonazepam - clonazepam online http://www.klonopinforsaleonline.com/.

    ReplyDelete
  77. 11, [url=http://www.radioflyercatamarancharters.com/]Lasix Discount[/url] - Lasix Online - furosemide for sale http://www.radioflyercatamarancharters.com/ .

    ReplyDelete
  78. 11, [url=http://www.klonopinforsaleonline.com/]cheap clonazepam no prescription [/url] - where to buy klonopin - purchase clonazepam online http://www.klonopinforsaleonline.com/.

    ReplyDelete
  79. 14, [url=http://www.tunesdaily.com/]Lorazepam online[/url] - Lorazepam Cost - lorazepam online http://www.tunesdaily.com/ .

    ReplyDelete
  80. 17, [url=http://tofilebankruptcyornot.com/]Cheap Zithromax[/url] - Azithromycin Online - buy azithromycin online http://tofilebankruptcyornot.com/ .

    ReplyDelete
  81. It is not my first time to go to see this web page, i
    am browsing this site dailly and obtain fastidious data from here every day.

    ReplyDelete
* Please Don't Spam Here. All the Comments are Reviewed by Admin.