shadowline
File Permissions << File Open Lock Read Write Unlock Close >> Creating Directories
The fopen command is used to open a specific file.

$myfile = fopen ("filename.ext","mode");

The value from opening the file will be transferred into the variable ($myfile).

Before opening a file however, you have to decide on what will be happening to the file :
1. Do you want to open the file just to read the contents?
2. Do you want to be able to write information to the file?
3. Do you want to delete the current contents of the file and replace them with new data?
4. Do you want to write to the start or end of the file?

Once you've decided on the outcome, the correct MODE is used in opening the file. The MODE part will set the open file for :
rRead only. Pointer set at top of file data.
r+Read and write. Pointer set at top of file data.
wWrite only. Any existing data will be deleted. If file is not found, PHP will create one.
w+Read and write. Any existing data will be deleted. If file is not found, PHP will create one.
aAppending only. New data will be written at the end of the existing file data.
a+Read and appending. New data will be written at the end of the existing file data.

Ok, the file is open and it is set to the mode of your choice. The next step is to lock the file. This prevents the file from being corrupted if more than one connection is being fed to it at the same time.

flock ($myfile, LOCK_EX);

It is now safe to read or write to the file depending on what you are actually having the file do for you.

// reading data from the file
$all_data = file ("filename.ext");

// writing some data to the file
fwrite ($myfile, "$newdata\n");

The data has been exchanged one way or another. Since we are done with the file, it should be unlocked and closed.

flock ($myfile, LOCK_UN);

fclose ($myfile);
File Permissions << File Open Lock Read Write Unlock Close >> Creating Directories
shadow

Advertise in the banner along the top, Click here!

Valid XHTML 1.0 Transitional Valid CSS! Text Link Ads
shadow
shadow