PHP
  Home arrow PHP arrow Page 3 - PHP 101 (part 5) - The Wonderland Fact...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP 101 (part 5) - The Wonderland Factor
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2000-09-06

    Table of Contents:
  • PHP 101 (part 5) - The Wonderland Factor
  • So Who Are You, Anyway?
  • Graffiti For The Masses
  • Calling Godzilla
  • Q
  • Arguments And Responses
  • The Wonderland Factor

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    PHP 101 (part 5) - The Wonderland Factor - Graffiti For The Masses


    (Page 3 of 7 )

    So much for reading files - how about actually writing one? In PHP, this is somewhat more complex, involving as it does the use of file "pointers" to write data to a file.

    The function that does most of the work here is a little munchkin called fopen(), which opens a pointer to the file. fopen() requires two parameters - the name of the file, and the mode in which the file is to be opened. This "mode" is one of the following:

    Read-only mode, activated by the parameter "r". Here, the file pointer is initialized at the first line of the file.

    Write-only mode, activated by the parameter "w". Here, the file is created anew and the pointer is initialized at the first line of the file. If the file already exists, all its contents are lost.

    Read/write mode, activated by the parameter "r+". Here, the file is opened for reading and writing, with the pointer initialized at the first line of the file.

    Append mode(write only), activated by the parameter "a". In this case, the pointer is placed at the *end* of the file, allowing you to append new content to the end of the file. If the file doesn't exist, a new one is created.

    Append mode(read/write only), activated by the parameter "a+". Here too, the pointer is placed at the *end* of the file, allowing you to append new content to the end of the file. If the file doesn't exist, a new one is created. You can also read from the file.

    Once you've got the file opened in the right mode, it's time to actually write something to it with the fputs() function. Our next example demonstrates this more clearly:


    <?php // set the file name $filename = "/tmp/myfile.txt"; // open the file $handle= fopen($filename,'a'); // create the string $string = "This is a test of the Emergency Broadcast System. If this had been an actual emergency, do you really think we'd stick around to tell you?"; // write the string to the file handle fputs($handle, $string); // close the file fclose($handle); ?>
    In this case, we've first opened a file pointer to the file; next, we've used the fputs() function to actually write a string to the file. The fputs() function needs two parameters - the file pointer to use, and the string to be written to the file. Once your file has been written, you can close the file with fclose(), and check the contents with readfile() or file().

    Our next example combines everything you've just learnt to create the world's longest story - check it out!

    <html> <head> <basefont face="Arial"> </head> <body> <?php // set the file name $filename = "/tmp/graffiti.dat"; // open the file $handle= fopen($filename,'a+'); // write the string to the file handle fputs($handle, $graffiti); // close the file fclose($handle); ?> <form action=graffiti.php4 method=get> <input type=text size=30 name=graffiti> <input type=submit name=submit value="Add your two bits!"> </form> <?php // display current contents of file if available if (file_exists($filename)) { echo "<b>Current graffiti reads: </b>"; readfile($filename); } else { echo "File not found!"; } ?> </body> </html>
    In this example, the user is presented with a simple form containing a text box. Once the user enters something into the text box and submits the form, the text string is inserted into the file "graffiti.dat". The page is then reloaded, the file is read and the complete contents displayed; the user now has the opportunity to enter something new into the box and have this text appended to the end of the existing text in the file.

    More PHP Articles
    More By Vikram Vaswani and Harish Kamath, (c) Melonfire


     

       

    PHP ARTICLES

    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT