PHP
  Home arrow PHP arrow Page 2 - Reading, Writing and Creating Files in PHP
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

Reading, Writing and Creating Files in PHP
By: Jacques Noah
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 34
    2006-08-23


    Table of Contents:
  • Reading, Writing and Creating Files in PHP
  • File Permissions in a Unix Environment
  • Writing to Files
  • Reading from Files

  • 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


    Reading, Writing and Creating Files in PHP - File Permissions in a Unix Environment
    ( Page 2 of 4 )

    In a Unix environment, all files and directories are owned by two different entities -- a user and a group. Each file in the File System has three permission sets that determine who can access a file or directory. These sets are: group-level, user-level and global-level. Each permission set has three flags: read, write and execute. If  a user does not have, for example, a read flag set, he or she will not be able to read a particular file or directory. The same applies to a user that does not have a execute flag set; he or she will not be able to execute that script.

    How to set the permissions

    Permissions are set using a command called CHMOD. CHMOD stands for CHange MODe. CHMOD comes as a set of three numbers. Each of these three numbers is a sum total of three other numbers. So you have to add three numbers to get the first CHMOD number, add three numbers to get the second CHMOD number and add three numbers to get the third CHMOD number. 

    Let me explain. Each digit is a number value from 0 to 7. The value specifies what capabilities are available (or not). These numbers correspond to three command types: read(r), write(w) and execute(x).

    Read (r) has a value of 4. It allows listing files in the directory.
    Write (w) has a value of 2. It allows the addition of new files to the directory.
    Execute (x) has a value of 1. It allows access to the files in the directory.

    Possible combinations available using these command types include:

    Digit

    rwx

    Result

    0

    ---

    no access

    1

    --x

    execute

    2

    -w-

    write

    3

    -wx

    write and execute

    4

    r--

    read

    5

    r-x

    read and execute

    6

    rw-

    read and write

    7

    rwx

    read write execute


    Sometimes you'll hear people say "chmod to 775."
     Okay... so what do the three digits stand for?

    The first digit represents the host server/computer. This will usually be set to seven. The second digit represents the group. And the third represents the world or "others."

    How do we arrive at the number 775? For the group "owner" we have:
    1 [execute access] + 2 [write access] + 4 [read access] = 7.

    For the group "groups" we have:
    1 [execute access] + 2 [write access] + 4 [read access] = 7.

    For group "others" we have:
    1 [execute access] + 0 [no write access] + 4 [read access] = 5.

    This means that the "host" and the "group" can do anything to the file, but anyone else can only execute it or read it. They can't modify it.

    How and when do you use the CHMOD command?

    The easiest way is by using an FTP program. Most FTP programs have a  right-click menu that allows you to set the CHMOD on a specific folder. I use DreamWeaver 8; it provides a GUI on which I can set the permissions by right clicking on a file or directory, as below:

    Fig3. CHMOD example



     
     
    >>> More PHP Articles          >>> More By Jacques Noah
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - 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





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