PHP
  Home arrow PHP arrow Page 3 - PHP 101 (part 5) - The Wonderland Factor
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

PHP 101 (part 5) - The Wonderland Factor
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek