7:40:09 AM
Thursday
Sep 09
Su M Tu W Th F Sa
567891011
12131415161718
19202122232425
262728293012
3456789

Play My Typing Game

View Some of My Artwork

Writing a Text File
When I was 13 and programming in BASIC, a huge door opened when a friend taught me how to write save game data to file. There are many times when you might want to write a text file via PHP. Sometimes smaller amounts of save data is better kept in text files than stored in database tables. There are different modes of writing, the two most common are 'w' (normal write/overwrite) and 'a' (append mode). Append mode is great when you need to write an error log, or something similar. The PHP.net manual page for fwrite has a bloated example. Here's the straight forward approach that assumes no problems.

        $str = "Whatever content you want to write to file";
        $handle = fopen("path/to/file.txt", 'w');
        fwrite($handle, $str);
        fclose($handle);


If you get a PHP error or warning, its most likely due to folder or file permissions. Perhaps even to file ownership. The folder you are writing to must be of minimum permissions of 755. If that doesn't work, try 775. You could also do 777, but thats not good advice if you're using a shared hosting provider.


Read Previous:
Loops
Read Next:
String Chopping Functions
Table of Contents


Give a listen to my music...
My Best Recordings
How my services and skills can help you:
  • Global (via Email/Skype/AIM(ichat)/Yahoo)
    • Web Development Consulting
    • Custom Web App Development (PHP/MySQL/jQuery/CSS) Cross-Browser & OS
    • Cellphone App Development (Android/MobileWeb)
    • Music Lessons & Education for Beginner Guitar, Piano, Bass, which transfers over to many other instruments I am comfortable playing but not yet at teaching.
    • Music - Soundtrack Composition & Voice Work
  • Local
    • Most all the above, but also:
      • Music Performance (Harp, Voice, Guitar, Piano, Bass, Latin Percussion)
      • Recording Live Music Audio
      • Video Streaming live music and other live events anywhere that can provide wifi
      • Training Video Streaming to your venue's trusted soundperson
Skills:
  • Global (via Email/Skype/AIM(ichat)/Yahoo)
    • Web Development Consulting
    • Custom Web App Development (PHP/MySQL/jQuery/CSS) Cross-Browser & OS
    • Cellphone App Development (Android/MobileWeb)
    • Music Lessons & Education for Beginner Guitar, Piano, Bass, which transfers over to many other instruments I am comfortable playing but not yet at teaching.
    • Music - Soundtrack Composition & Voice Work
  • Local, Most all the above, but also:
    • Music Performance (Harp, Voice, Guitar, Piano, Bass, Latin Percussion)
    • Being your venue's soundman
    • Video Streaming live music and other live events anywhere that can provide wifi
    • Training Video Streaming to your venue's trusted soundperson