PHP
  Home arrow PHP arrow Page 2 - 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? 
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 - So Who Are You, Anyway?
    ( Page 2 of 7 )

    In the examples you've just seen, we've made one basic assumption - the file that PHP is trying to read already exists. However, in the big bad world, this assumption can sometimes cost you hours of debug time if the file isn't actually present on the system.

    To avoid situations like this, PHP offers the file_exists() function, which can be used to test for the presence of the file and generate an appropriate error message if it isn't found. Let's modify the example above to include this capability:

    <?php // set filename $filename = "random.txt"; // check for file if (file_exists($filename)) { // read file into array $contents = file($filename); // get array length $length = sizeof($contents); echo "<b>File $filename contains $length line(s)</b><p>"; // display each line with "for" loop for($counter=0; $counter<$length; $counter++) { echo "$contents[$counter]<br>"; } } else { echo "<b>File not found!</b>"; } ?>
    Now try it out - rename or delete your file, and watch as PHP gracefully exits with your customized error message.

    PHP also comes with a bunch of functions that can provide you with detailed information about the type of file - here's a list:

    is_dir() - checks whether the specified file is a directory is_executable() - checks whether the specified file is executable is_link() - checks whether the specified file is a link is_readable() - checks whether the specified file is readable is_writeable() - checks whether the specified file is writable filegroup() - returns the group fileowner() - returns the owner of the file fileperms() - returns the current permissions on the file filesize() - returns the size of the file filetype() - returns the type of the file
    And here's an example:

    <?php // set filename $filename = "random.txt"; // check for file if (file_exists($filename)) { echo "<b>$filename</b><br>"; // check if it is a directory if (is_dir($filename)) { echo "File is a directory
    "; } // check if it is executable if (is_executable($filename)) { echo "File is executable
    "; } // check if it is readable if (is_readable($filename)) { echo "File is readable
    "; } // check if it is writeable if (is_writeable($filename)) { echo "File is writeable
    "; } echo "File size is " . filesize($filename) . " bytes<br>"; echo "File group is " . filegroup($filename) . "<br>"; echo "File owner is " . fileowner($filename) . "<br>"; echo "File permissions are " . fileperms($filename) . "<br>"; echo "File type is " . filetype($filename) . "<br>"; } else { echo "<b>File not found!</br>"; } ?>

    Here's the output:

    File is readable File is writeable File size is 286 bytes File group is 100 File owner is 538 File permissions are 33188 File type is file


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

       

    PHP ARTICLES

    - 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 ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT