Home arrow PHP arrow Page 2 - Reading, Writing and Creating Files in PHP

File Permissions in a Unix Environment - PHP

Reading and writing to files can be useful if you do not require the storing of important data, such as a web counter. I must warn you though, that this method of storage should not be used to store passwords and other critical information, as it is not safe. Here we will discuss how to handle files and directories in PHP, specifically, how to create, read and write them.

TABLE OF CONTENTS:
  1. Reading, Writing and Creating Files in PHP
  2. File Permissions in a Unix Environment
  3. Writing to Files
  4. Reading from Files
By: Jacques Noah
Rating: starstarstarstarstar / 42
August 23, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: