PHP
  Home arrow PHP arrow Page 2 - 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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 - 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

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway