|
The operations of entry / exit in PHP have a big importance in any computer language since he has not sense that a computer language could not write, read, update information of a database, etc. In this chapter we will center basically on the operations of entry and exit with files and later we will explain the operations with databases.
Let's suppose that we want to do a buy shop online. Let's imagine the big effort that would suppose having to modify all the pages HTML of those products in which in the offer period his price was turning out to be affected. The most primitive solution for the information storage is a text file, the content of the file of text can be any.
How do we open a file?
To open a file PHP it puts at disposal a function. His syntax is the following one:
fopen (file, way); the route of the file is indicated in file, and way determines the different ways of reading of a file:
| Attribute |
Effect |
| r |
Alone reading |
| r + |
Reading and writing |
| w |
Only it registers. It erases the previous content |
| w + |
Reading and writing. It erases the previous content. |
| to |
Only it registers. It preserves the previous content. |
| to + |
Reading and writing. It preserves the previous content |
The function fopen returns a manejador of file that is the one that we will use in the functions related to the reading and writing of files.
How is a file covered?
To read a file logically the operation is very simple. He consists of being reading all the characters up to coming to the EOF (end of file... end of the file) which determines the end of the text.
As in the counterfoils the files possess an internal cursor that indicates his current position with regard to the whole text.
To verify if it has gone over at the end of the file, PHP puts at our disposal the function feof (manejador_fichero); that is in charge of verifying if the current position of the file is the mark of the end.
The most generic reading function is fread (manejador_fichero, nº_bytes); that is in charge of reading a certain number of bytes of the file and of returning it to us in a chain of characters.
On the other hand, if what we claim is to read the file character to character we must use the function fgetc (manejador_fichero);.
Others of the most typical functions are: fgets (file, nº bytes); that returns us a chain of characters with the well-read information. fgetss (manejador_fichero, nº bytes, [mostrar_tags]); he reads us a file HTML omitting the tags typical of the code, in showing tags we must introduce the tags that we wish them to appear.
If after it realizes the reading what there try to read themselves the values that continue a certain entry format we can save ourselves a big work using the function fscanf (manejador_fichero, format, variables); This function obtains the information of entry of a file following a certain format. Later the above mentioned information must be treated by the programmer.
The last function that we are going to study to open files is the function file (nombre_fichero); with this function it is not necessary to use the functions fopen () and fclose () since they are executed of implicit form.
How does a file close?
Closing is the last operation that it is necessary to realize on having manipulated a file and allows to close the reference that is done to the file in the executed script. To be able to close a file we use the function fclose (manejador_fichero);
It registers in files
To write in a file basically we must realize three operations: to open the file in way registers, to realize the operation of realized writing, to close the file. The functions that we use to write in a file are fputs (manejador_fichero, chain); and fwrite (manejador_fichero, chain); that allow us to add a chain of characters to the previous text contained in the text.
Direct access in files
There exists a set of functions that allow us to be located in a certain position of the file to read from here on.
The function fseek (manejador_fichero, position); he allows Us to begin reading a file from a position as it decides: seek_set (it takes with the beginning of the file), seek_cur (current position), seek_end (final position).
Two basic functions exist that us posicionan at the beginning of the file-> rewind (manejador_fichero); or that return us the current value of the position-> ftell (manejador_fichero);
Functions changed for the handling of files
fpassthru (manejador_fichero); it shows Us the content indexed for manejador of file. He returns the number of showed bytes if no mistake takes place.
set_file_buffer (file, size); It determines the size of the buffer of this file that PHP was using.
readfile (file); He reads a file and shows it for the method of standard exit.
|