We work with files on PHP

Now there is a huge quantity{amount} of means for creation on

To true interactive a web of applications. Among the most popular it is necessary to note asp, perl and php. These means have strongly come in a life of the web - developer. On given momentsamym language php is quickly developing. Therefore we shall consider his  opportunities in more detail.


php is introduced language of scripts. Much of his  syntax

It is borrowed from c, java and perl with some, incorporated in him , unique

Features. The purpose of language will be to allow webs - developers to create quickly dynamically generated pages.


Development of the Network dictates the requirements on granting to the user

His  interesting information. The site is not only a set of webs - pages. It also means for storage of the information, the organization of transaction processing of the user and creation of the adequate answer to the user search.


To store{keep} the information on the server it is possible in several ways. The first - with

Use of databases. Very convenient way of storage of great volumes

Information when it is necessary to organize samples to sort the information on various parameters, etc. If it is necessary to process small volume

Information, moreover and without any shifts (for example to organize the account

Visitings) the second way - accommodation of the information in files will approach.


php gives very big set of means on job with files and file

System. We shall consider more in detail some functions for job with files.


Naturally, for the beginning jobs with a file of it  should be opened. For this purpose

Function fopen () is intended. This function can be used for opening any file in file system of the server through http or ftp:


int fopen (string filename, string mode);


filename - a name of an opened file, mode - the index on a mode of opening of a file.

He can accept one of the following values:


Value the Description a To open a file only for additions. The data are finished in

The end of a file. a + To open a file for addition and readings. The data are finished in the end of a file. r To open a file only for reading.


r + To open a file for reading and additions. The data are finished in the beginning of a file. w

To open a file only for recording. The existing data will be lost. w + To open a file for reading and recordings. The existing data will be lost.


At job with a binary file, it is necessary to specify a flag b. However, if between

By binary and text files it is not done{made} distinctions (as in system unix), this

The flag is ignored.


At successful performance function fopen () returns a descriptor of a file, which

Represents an integer. This descriptor is intended for links to a file

By the subsequent calls of functions of job with files. At failure comes back false.

Example:



$file_hendel = fopen ("report", "a"); if (! $file_hendle) {echo (" It is impossible

To open a file... ");} else {// functions of job with files;}


After end of job with a file it is necessary to close it :


int fclose (int fp)


As argument, function fclose () accepts a descriptor closed

File. In case of success comes back true, in case of failure - false.


Simply so files to open senselessly. With them it is necessary to do{make} something. First,

That occurs - to display contents of a file. This operation carries out

Function fpassthru ():


int fpassthru (int fp);


This function carries out reading from the current position in an opened file and up to it

The end then she closes a file. In case of success comes back true, in a case

Failures - false.


There can be necessities to deduce{remove} all file. Probably, to us

It is required to read only a part of the data, for their use on our page. For this purpose in Rhp there are some functions.


For reading the set quantity{amount} of symbols from an opened file, it is possible

To use function fread ():


string fread (int fp, int length);


Function reads a line in length length symbols from a file with a descriptor fp.

Example:



$file_hendel=fopen("text.txt "," r "); if (! $file_hendle) {echo (" It is impossible

To open a file... ");} else {$text = fread ($file_hendel, 5); // It is read out the first 5

Symbols fclose ($file_hendel);}


If the end of a file achieve before length length all comes back

The read text.


As functions are applied to data reading from a file fgetc (), fgets () and

fgetss ()


string fgets (int fp, int length); string fgetss (int fp, int length);


Function fgetc () makes reading one symbol.


Function fgets () returns a line from (length-1) symbols. Reading comes to the end,

If the symbol of translation of a line or the end of a file will be achieved.


Function fgetss () is identical fgets (), but all tegi html leave from a line, but

Are taken into account in length of a line length.


At a mistake these functions return false.


Also, for reading contents of a file it is possible to use function file (),

Returning contents of a file as a file. Each line of a file is represented as an element of a file:


array file (string filename);


Function accepts as argument not a descriptor of a file, and a line with a name

File.


For recording in a file functions fputs () and fwtite () are used.


int fputs (int fp, string string, [int length]); int fwrite (int fp, string

string, [int length]);


If last parameter is not specified, recording all the line long is carried out.

Example (the account of visitings with entering the data on date of visiting and ip the visitor in a file

And a conclusion to the screen of the message on number{room} of visiting):



<? php


// The account of visitings // quantity{amount} of visitors we shall store{keep} in a file report

$date=date (" d m y, h:i:s "); $ip=getenv ("remote_addr"); // opredljaem ip $host site

= gethostbyaddr ($ip); // we determine a hostname on it  ip $str = (// we form a line,

date - $date // for its{her} accommodation in a file host - $host // the account ip - $ip

---------------------"); $file_hendle = fopen ("report", " a + "); // we open a file

report for additions, fputs ($file_hendle, $str); // it is written down in him

The prepared line and fclose ($file_hendle); // we close a file


// Opredelnie and a conclusion of a serial number of the visitor // quantity{amount} of visitors

Let's store{keep} in a file visitors


// We open a file visitors for rewriting $file_hendle = fopen ("visitors",

" w + "); $visitor_number = fread ($file_hendle, filesize ("visitors"));

$visitor_numbe ++; // we add one more visitor, rewind ($file_hendle);

fwrite ($file_hendle, $visitor_numbe); // we copy a file visitors and

fclose ($file_hendle); // we close a file echo (" we Congratulate, you $visitor_numbe

The visitor! ");


?>


There are also some functions for management of the current position in a file.

They facilitate free moving on a file.


Function rewind () establishes the current position in the beginning of a file, a descriptor

Which she accepts as argument.


There can be a necessity to define{determine} the current position in a file. For this

The purpose is served with function ftell ()


int ftell (int fp);


With the help of function fseek () it is possible to move to the set position.


int fseek (int fp, int offset [, int whence]);


The basic arguments of function are the descriptor of a file and value set

Positions from the beginning of a file. For example, about the help of function ftell () it is possible to organize

Moving to the set position:


fseek ($fp, ftell ($fp) +10); // transition to 10 positions forward from current


But the same effect in php4 can be achieved without use of function ftell (),

As in function fseek () the unessential argument which defines{determines} has appeared

Way of moving on the set position: seek_set - counts value from the beginning

File (by default); seek_cur - counts value from the current position; seek_end

- Adds value offset by the end of a file.


The same example in php4 will become:


fseek ($fp, 10, seek_cur);


Often there are situations when it is necessary to check up position of the index

Concerning the end of a file. Function is applied for this purpose feof () which returns

true if achieve the end of a file and false if the end of a file do not achieve.


Opportunities php on job with files are not limited resulted here

Functions. With the full description of functions for job with file system it is possible

Will familiarize on an official site php in section php: manual: filesystem functions.