Home
Home Page
PHP and Web. Caching
Job with Cookies on PHP
Electronic dispatches
JUzabiliti the main page
Natural keys against artificial keys
Uniform autentifikacija Windows NT/2000 and Oracle
The manual on Link Popularity
Partner Links: optimize an exchange of links
What for registration in catalogues through 1PS.RU is necessary
Krossbrauzernyj DHTML
DHTML-skriplet - it is simple about simple
Promotion of a site with the help of bulletin boards
The practical grant{manual} on a spelling of slogans for websites
We check the site - that has taken place with your ranging?
PHP: Patterns
Use of patterns in PHP4
Really easy change of design
Job with patterns, use HTML-Template with CGI-scripts
Job with files in PHP
Links
 

Job with files in PHP

In programming language PHP there are functions for job with fajlmi. In this clause{article} we shall consider them...

Inclusion of files in the document


Sometimes it is necessary us often changeable elements of the program or the page to bear{take out} in a separate file, and then it  dynamically to include in the document... It can be the list often changeable variables or, for example, the menu. To switch on these files to us function include () will help. She has unique argument which should be way to the necessary (included) document.


Example


We bear{We take out} the menu of our site in a separate file. We shall name it  ' menu.txt ' and we shall place in the same folder, as index.php, and instead of a code of the menu it is entered

<?

include ("menu.txt");

?>



Works? Wonderfully!

Check of existence of a file


Often it will be necessary for programs to check up, whether there is a necessary file. For this purpose function file_exists () is thought up. Function returns true if the file exists and false if no.


Example

<?

if (file_exists("file.txt ")) {

//.. The Further actions...

} else {

echo (" File.txt does not exist! ");

}

?>



We create and delete files


For creation of files there is a function touch ().


Example

<? touch ("file.txt");?>



And for removal{distance} of files function unlink () is stipulated.


Example

<? unlink ("file.txt");?>



Reading, recording and addition in a file


For opening a file there is a function fopen (). 2 arguments are passed her. The first - a way to a file, and the second - a mode of access. Modes of access is 3. The first is designated "r" - the file only for reading, but not for the recording, the second "w" - recording a file opens (!!! At recording the information in a file will be erased also the new data will be written down in the beginning of already empty file!!!). If to use a mode of addition in the end of a file designated by the letter "a" the data will enter the name in the end of a file, and the current contents remain.


It is better to appropriate{give} an open file of a variable which then will be the index on a file.


Examples


Opening of a file for reading

$file=fopen("file.txt "," r ")



Opening of a file for recording

$file=fopen("file.txt "," w ")



Opening of a file for addition of the data in the end

$file=fopen("file.txt "," a ")



After job with a file of it  it is necessary to close, differently all changed in a file will not be saved by process:

fclose ($file); // $file - the index on a file



Reading of lines from a file


For reading lines from a file there is a function fgets (). It{she} has 2 arguments. The first - the index on a file, the second - the maximum quantity of symbols which can be read before will meet the end of a file or a line (usually it is "1024").


If you want to read all file and to deduce{remove} his  contents in a browser, you need function feof ($file), where $file - the index on a file. She returns true at achievement of the end of a file and false otherwise.


So, to read the document, we use a code:

<?

$file = fopen ("file.txt", "r");

while (! feof ($file))

{

$text = fgets ($file, 1024);

echo ($text)

}

?>



If you want to read certain{determined} kolichesvo symbols use function fread ().


Example

<?

$file = fopen ("file.txt", "r");

$text = fread ($file, 25);

echo ($text);

?>



For moving on a file (to be exact, displacement) function fseek (the index on a file, quantity{amount} of symbols on which it is necessary to recede from the beginning of a file) is stipulated.

fseek ($file, 80);



Recording in a file


For recording in a file we use functions fwrite () or fputs () which anything from each other do not differ.


Example

fwrite ($file, " your text ");

fputs ($file, " your text ");



Certainly, it is necessary to open all over again a file with a corresponding mode of access ("w" or "a").

We block a file...


Now there is a question. And what if some processes will simultaneously write down the data in a file? Yes - yes, it will be bad. The data will be hashed in a huge heap and will turn in the ordinary information dust which is not giving in to decoding. Fortunately, on this case there is a blocking of a file. There are 2 kinds of blocking - partial and full. Partial forbids to other processes to write to a file the data, but does not forbid to read them, and full forbids both reading, and recording.


For blocking flock (the index function responds on a file, a kind of blocking). The kind of blocking is designated by figure (1 - partial, 2 - full). To unblock a file pol`zujes` function flock, but a kind of blocking put "3".


Success to you and your programs;)!