You are on page 1of 21

SESS1ONS-

A Sesston s the tme span durl whtch a

bnowse nteyace Panttcudan serven


A Sesslon begtns uhen a bnowsen becoma

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

8t0üng data ih sessfon:

*By the nakure webpages halds only tempoaany


Store Houn data on the
data uhtess tou spectttcally4
Seven.
One way
way Stong data on the seve s
the to

Ube bessfon using bassfons You can Stone and

Atrteve data name.

To w0ak uuith a sesston you stat by

Calleng Segsfon-Start:

Sessfonstat C)
Sosslon use the
TO store data h the

example, here we we2 Stoitng


$SESSION aomay 4o
u nden the ey MOVte
Read wfndow
Readutndouw "; Now n
$SESIONI morte ]

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

Sored yon PUnchase, $ 39.35


To Aead yow Puchase hahew pae,dick hue

Jooking on entsts, fou


Tthe data you aTe

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

Nelcome.You punchabe b $A.a5


File Handltng:

seaven-sfde tchnologt I t is able o


Php a

wdte 4tes n the Seven sstems.


| Create ead and

PHp Can deal with the 91esidlg on


any seAVA

bath" http and


Sstem and the intenact
intenat alng

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

the tle an the 4he vaüable.

Evety 4tle has an interval. pointes s


oper
Used t, hdiate uhe the heat cpanakors

shauld take place uhthin he 4e we call this

Poircer the tle pointen.

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

ot edst, attempt o create t .

w operv pa neading and witAng and


tuncate the t e to zevdength. the
ftte does ot enksb, attempt to create t ,

oper p appendtng ony. I the filk


doas o t extst, attempt do Cte l .

a+ Open toa aeadtng and wilting, Stanttng a


the end the 4 . I the ile des hot

eaist, akempt to ceate t .


Create and eperv p witng enly. 4
the le alneady éxtsts the
,
fopen call
totu 4a y ehunin FALSE

Create and, open


fo neadtna and wiRa
the Lle aseady extsts the kopen
call wu fa
onetnring FALSE.

t asstble pathe fopen unctfor o fal


oa etample an atlempts made to ofen a 4e toa

Suh e edsts ould also


veading out so

the pe actess peisfo dtd na alo


4al e
he eutred.

The open Rinctfon Aetunns FALSE FALSE

and stops the


t Simply piodces a message

ahe use' a dte.


artenpaetotfon process (wn

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

anamete . The les names 4 atun6 TRUE 4

the 4te extsts FALSE athenuise

unta

pilexists C"pilenarme ")


Example
html
head7 4HHe7 opening a fil 4ltfte7 zlhead7
Abedy
hi7 opering a fle <lhiz
2php
handle fopen C"ftle.. tat, 'a")
4C4handle)
echo FiL opened, ok.
27
<lbody7
html
Output

@opontng a frle php


Cpentng a fle

Fle opened, ok.

Readtna Fom a Fle -

to ead a fiz i
The mosS Common way
bntet nto sealar vasiable
PHp o mOYe

% a sting.

The fread. Aunctfon aeads path an all at a

a stsdng uhat uas ead,


file and 9netu-ens

This hunctfon akes wo panameters,

A fe vastable.

t) The numben at oytes insoamation

Synta:

inavariade fnead (ffile _variaHe, numben al bytes)3|


*The neadfng openation Stops when eithen
maukes a6 9ead e the
the end o e
numben 4 b y t e s has been ead.
Spectyted
data ae Okten Stoaed
Lange eollectfon a
database Sstems. So only Small, data, Sats

ae stoned

Example:
4ktnl7
head Rtte Reading from a file 41ttey 4lhendz
Kboy7
centen7
<2php
$handle =
fopen C"4le.+tt ", *a");
uohtle (feog ($handie))

Stent faets (4handie)


echo $text"<bn7"s

lbody7
Alhtrl7
file.tat

Here

data
cloaing a le :

clased wth he pclose Auncton


A tle
uktch takes a file vastabe as ony
ony |

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))

4text fqers (¢handle)s


echo 4et, '<b17*

falose (4handle)
97
4lbody
4l htmly
Output:

@Reading from afilu.php

rom a fle
Readtng
Hene

youn
data

Reading a file nto an anay wth le

One ateinative do read is 4e uuhteh

akesa ile name as dspaNameten and neuans


an anay he Jenes o the 4le.

Syntai

4FiL aay - f C"flu name ");

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)

echo LEne $number : ",3Lne, "zbn7"


4l body
htaml7
Olttput :

@Readingafle an awiay. php


Readtng a fte hto an aNay

ne o t Hene

Lene is
Kinc 2 Youn
Lne3 data

Reading dext nom a l e wslng t a e t s

A A SHgle dtne t aie can pe nead wth

fgets which takes two panameens.

vantable
t The tle
the ne to be
A mit on the Jength a

veadi
&ynta9t

4 eline_vaniable =fgets (4fie_vasiable,, Jenqth )

Example

49php
$handle = fopen C'fla . tnt ", "n")
whle Cfeoh (4handie))

text4qets C4handle)

97
a 4tle chanacten by hanacten wih
Readtng 4om

etec)

Ne Can nead nditdual chanacte 4rom

text e tustng the fgetc c)

Suntaa

faeteCrle vaniable);

Example
<html
headr <Hte7 Readirg chasnactes fom a flle Htte7
l head
body
h7 Readtng ehanactrs From a fle <lh1y

42php
"

$handle fopen ( "£ile.tatt "a")5

uwhile ($chan - fqetc ($handle)

Cgchan = = " In")

$Cha '<baz"s

echo"4chau";

felose (4hand le)


7
4 lbody7
output:

Readtng chanactonsfrom a flle.-php

Reading chanactens fnom a pile

Hene

data

Getting e size nith l e atye :

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)

F l ofunetfon aetned as an itegea.

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.

Sobtina the 4ile pointen 's locatfon uith Seek

* PHp % Uses de poirten o keep iack

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.

hen u open a {ie en appending the te

Poiten Set the end the le


Symta:

f300k Chandle,ofeaet,[ tant potnter]) ;


handle - Js the handle a he 40e to ee the l e

pointenh
OAESet - I s the humben o ytos you uwant to

sek the aperaten pointw.


Start-point - Indicates a startng poivt foa the

pointen wich one hesa Constants.

SEER-SET the le
keg?rîng athe

locatfon.
8FEK-CUR>The aurreht poirt
SEEK-END The end a the 4e.

Copybhg files wêth cop


Can oP es uth dthe cop

functfon

Stan:

COPy (0ece, desttnatfon)

the souce tue.


Souce Name e

Desttnaton Name c he copy.


This functon aetws TRUE Of wa A

Sutessfu FALSE theyuse.

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

ech ould rat coP Fte

7
4ody7
lhtml7

Output:

@Copingftes-php
Copybng rles
opted. fa.tat
Deletfna files wih unlink:

a file Ustng the


Ne want delete

unltnk unctton

Sunta

nlink (filename [ contextJ)

lename Name et the l

Context optBonal context

TRUE the l e s
Thfs Punctfoh netunns

FALSE Othenwtse.
Cas deleted

EXample :

headr <Rtte7 Deletfng ftles <14tHe7 <lhead7

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

Delcted the ftie.

Natfng to a filewih fonite-

Ne want to ite a strlng do a f

tue Could use fuuite.

Sytaz

fuosite Cfilk -vaxtale ing engthJ) |

*PHP has a Single functfon to ute to

a fie waita. It takes t o parameteso

*he stsdng do ba itten to the e .

*he fuwnte functfon aetushs the


the numben

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) = =

echo "can nat wite data.trt "

else

echo wsited data.tat ";

felose c4handle) ;

<lbody7 zlhtnd7

@rittng ftles-php
wHng fTles
hote data. txt

You might also like