You are on page 1of 3

Functions Used in File Handling

1) fopen(filename,mode) - used for opening a file with specific mode.


2) fclose(fp) - used to close the file. (fp -> filepointer)
3) feof(fp) - used to find the End of File
4) fgetc(fp) - ets character from file pointer
!) fgets(fp) - ets a line from file pointer
") fread(fp,si#e) - $inar%-safe file read
&) fscanf(fp,format) - 'arses input from a file according to a format
() fwrite(fp,string) - $inar%-safe file write
)) file*put*contents(fp,string) - +rite a string to a file
1,) file*get*contents(fp) - -eads entire file into a string
11) file(fp) - -eads entire file into an arra%
12) file*e.ists(fp) - /hec0s whether a file or director% e.ists
13) is*reada1le(fp) - 2ells whether the filename is reada1le
14) is*writa1le(fp) - 2ells whether the filename is writa1le
1!) is*file(path) - 2ells whether the filename is a regular file
1") is*dir(path) - 2ells whether the filename is a director%
1&) is*lin0(path) - 2ells whether the filename is a s%m1olic lin0
1() readlin0(path) - -eturns the target of a s%m1olic lin0
1)) readdir - -ead entr% from director% handle
2,) glo1 - Find pathnames matching a pattern
21) filesi#e(fp) - ets file si#e
22) filet%pe(fp) - ets file t%pe
23) fprintf(fp,format) - +rite a formatted string to a stream
24) fstat(fp) - ets information a1out a file using an open file pointer
File Opening Modes
3odes 4escription
r -ead onl%. 5tarts at the 1eginning of the file
r6 -ead7+rite. 5tarts at the 1eginning of the file
w +rite onl%. 8pens and clears the contents of file9 or creates a new file if it doesn:t e.ist
w6 -ead7+rite. 8pens and clears the contents of file9 or creates a new file if it doesn:t e.ist
a ;ppend. 8pens and writes to the end of the file or creates a new file if it doesn:t e.ist
a6 -ead7;ppend. 'reser<es file content 1% writing to the end of the file
. +rite onl%. /reates a new file. -eturns F;=5E and an error if file alread% e.ists
.6 -ead7+rite. /reates a new file. -eturns F;=5E and an error if file alread% e.ists
File Reading and Writing
3ethod 1 > File -eading ?sing $%tes
@Aphp
7B
B ;uthor > ;nton%
B /reated on 22-Fe1-,(
B 'rogram Came > fileread1%tes.php
B7
Dfilename E Fsample.t.tF9
Dfp E fopen(Dfilename,FrF) or e.it(F?na1le to open file...F)9
while(Gfeof(Dfp))
H
Dcontent E fread(Dfp,filesi#e(Dfp))9
echo Dcontent9
I
fclose(Dfp)9
A>
3ethod 1 > File -eading ?sing /haracter
@Aphp
7B
B ;uthor > ;nton%
B /reated on 22-Fe1-,(
B 'rogram Came > fileread.php
B7
Dfilename E F7home7anthoniraJ7te.tfiles7sample.t.tF9
Dfp E fopen(Dfilename,FrF) or e.it(F?na1le to open file...F)9
while(Gfeof(Dfp))
H
echo fgetc(Dfp)9
echo F@1r 7>F9
I
fclose(Dfp)9
A>
3ethod 1 > File -eading ?sing =ines
@Aphp
7B
B ;uthor > ;nton%
B /reated on 22-Fe1-,(
B 'rogram Came > filereadline.php
B7
Dfilename E Fsample.t.tF9
Dfp E fopen(Dfilename,FrF) or e.it(F?na1le to open file...F)9
while(Gfeof(Dfp))
H
echo fgets(Dfp)9
echo F@1r 7>F9
I
fclose(Dfp)9
A>
3ethod 4> +riting /ontents to File
@Aphp
7B
B ;uthor > ;nton%
B /reated on 22-Fe1-,(
B 'rogram Came > filewritechar.php
B7
Dfilename E Fsample.t.tF9
Dfp E fopen(Dfilename,FwF) or e.it(F?na1le to open file...F)9
Dcontent E F5ample File /ontentsF9
if(fwrite(Dfp,Dcontent)EEtrue)
H
echo F5uccessfull% writtenF9
I
else
H
echo FFailed to writeF9
I
A>
Simple Counter Program
@Aphp
7B
B ;uthor > ;nton%
B /reated on 22-Fe1-,(
B 'rogram Came > counter.php
B7
Dpath E Fcounter.t.tF9
Dfp E fopen(Dpath, FrF)9
Dcount E fread(Dfp, 1,24)9
fclose(Dfp)9
Dcount E Dcount 6 19
echo F@p>'age <iews> F . Dcount . F@7p>F9
Dfp E fopen(Dpath, FwF)9
fwrite(Dfp, Dcount)9
fclose(Dfp)9
A>

You might also like