Professional Documents
Culture Documents
PHP UNIT-4 PART-2 (Sessions & File Handling)
PHP UNIT-4 PART-2 (Sessions & File Handling)
Connected to a pottuln,
Tt ends when the bnouwse cheenpcked o be
Connected to that seve bcause ethen & beomnas
Connected
a dtfferent Seven on t s temtnated..
Connect
Calleng Segsfon-Start:
Sessfonstat C)
Sosslon use the
TO store data h the
access ethen a he
Jhe cheme on
another pages
acess the data uhlen
dGhoent page. Youu Can
the key
key Novte" using 4-SrsSION
Example
html
head
Ktittey Stoing data in sessfon KItitle
head
body7
hl7 tosing data n Sessfon <lhl7
<2php
Session-Stant C)
$SESSION 'Punchase'J =34.25
27
Sto Ted youst punchase 3-35
b7
To 1ead new paqe,
PwIchase
a hreg "phpsesston a. php
=
clfck hene <la>
Zlbody>
Zl html
Output:
hpsessfon php
stpsdng data in sesstons
thts
Canv dlsphay
Example
htrmd <1tfHee
data from sessfons
head <tte7 Retrieving
lhead
body
data from 3essfons 1h17
h7 Reisteving
49 Php
Sessfon- Stat C
["pnchase"]
Ctssat C4-SESsiON
\¥.$-sSESSION[Puxku)
Nelcome. You havepuehasad
echo
."ooth."i
4body7
htm
Output:
@phpsessfon 2. php
Rebuieving data from esstons
FTP Paotonl.
e dnple marted
AL 4ies opeatfons PHP
ve fanction6.
i-
Opening a ile
le
The st step
to
that Prepanes the te
aa pooess
open
ths paognamn Valabls
P future nejenence
the le Vaxiable.
Called
performS thasse
The fopen C Runctfon
two nameters,
Operatfons. t takes
path to &
the
The lename hcudenq9
Ln a d4tenent dinetoAY
Spactyt es the
ndicatan Cmode) who
fA
opendtion
Both anametens aTe forer stodng
The fopen C) unctfon netuins the najerence
MOde
Meanig
open neadtng on
open fos1 neading and wrlRng
open on woiting onty and tun cate
he4tle 0 zeno Jength I the e does
Byrta:
dte("vu0
4 flkvariable -fopen. ("4lename, made or
messaq"D;|
extsts with file-enist :-
checking a jle
the pocblem fopenfaLltng because the
does nat exdsts Can e 5 avotded
Spectted tles
the teosts oith
by deteminêng wheathen
4te e s t s befona calleng9 fopen
The l e - ertsts huncton taken a single
unta
to ead a fiz i
The mosS Common way
bntet nto sealar vasiable
PHp o mOYe
% a sting.
A fe vastable.
Synta:
ae stoned
Example:
4ktnl7
head Rtte Reading from a file 41ttey 4lhendz
Kboy7
centen7
<2php
$handle =
fopen C"4le.+tt ", *a");
uohtle (feog ($handie))
lbody7
Alhtrl7
file.tat
Here
data
cloaing a le :
panameten.
8yntaa
fclose rle-vaolable):
Example
html7
headr 4Htte7 Readtng nom a ile 41ftez <lhead
cbody7
4lhiz
4h7 Reading aom a tfie
42php
Shandle fopen. C"4le .tat", 'n")s
tohdle C feof (¢handle))
falose (4handle)
97
4lbody
4l htmly
Output:
rom a fle
Readtng
Hene
youn
data
Syntai
EXample
hdtml7
head7 (RtHe7 Reading a fle to an aay <ltite7 <lhead
body7
<lhi7
Ahi Reading a Afie nto an amay
42 php
sdata 4ile C"fle.tot")
foneach (data as 4umber => $lfne)
ne o t Hene
Lene is
Kinc 2 Youn
Lne3 data
vantable
t The tle
the ne to be
A mit on the Jength a
veadi
&ynta9t
Example
49php
$handle = fopen C'fla . tnt ", "n")
whle Cfeoh (4handie))
text4qets C4handle)
97
a 4tle chanacten by hanacten wih
Readtng 4om
etec)
Suntaa
faeteCrle vaniable);
Example
<html
headr <Hte7 Readirg chasnactes fom a flle Htte7
l head
body
h7 Readtng ehanactrs From a fle <lh1y
42php
"
$Cha '<baz"s
echo"4chau";
Hene
data
he
To get the Coanect le Jength nwih
slze functfon
The te alye hunctfon takes a
shgle
the name t h e lle Cno
the 4
Pamere2
vakable)
ptleatge"¢floname "):
Example
html7
2/tftte7 Zlhead7
head7 4RHey GeHtng tle stze
kbody '
Sfz lhiz
4ht7 Geting 4le
9Php
echo Thele 4.tut ' , iastza C"u.tnt"),
bytes ong
97
2l body
kl html
Outpeut:
@gattingfiustge php
Geting l siu
The 4la 4lL. tt i 24 bytes lang.
she neat
ahd aherc
where a le
write cpenatfonn OCcutstom.
Nead 0A
pairte s set
dwhen you oper a file the le
to the bagining o the u.
pointenh
OAESet - I s the humben o ytos you uwant to
SEER-SET the le
keg?rîng athe
locatfon.
8FEK-CUR>The aurreht poirt
SEEK-END The end a the 4e.
functfon
Stan:
Example:
html
<head7 <ttle> Copyeng tües <ltHey <lheedy
ody7
hiz Copyhg talus <lhiz
9php
$FMeftle.tat
$Py copy tt "
4CCoPy C4fte, $copy))
echo copled. $tfe "
3
else
7
4ody7
lhtml7
Output:
@Copingftes-php
Copybng rles
opted. fa.tat
Deletfna files wih unlink:
unltnk unctton
Sunta
TRUE the l e s
Thfs Punctfoh netunns
FALSE Othenwtse.
Cas deleted
EXample :
Abody
Ples <lhiz
htr peleing
22php
Cunltnk C'Copy tut ")
echo"Peleted the le '
3
else
echo coud nat delete the file ";
97
4lbody7
lht ml7
output
@deletfngfssphp
Deleting Fls
Sytaz
an
4dytos wükten c FALSE thee werL
Fample
khtml 7
<head
fTAes <lbRtte7
44fttez Waúttng
theadr
hody
Lcentenz7
lhiz
huz witfng HCtes
<lcenten7
43 php
shandlefopen C "dara.trt ', "uw ");
=
t e n t . "s
t e t "Hene \n \ n the
FALSE)
Cfodte (Çhandte, ètext) = =
else
felose c4handle) ;
<lbody7 zlhtnd7
@rittng ftles-php
wHng fTles
hote data. txt