Professional Documents
Culture Documents
Chapter Three1
Chapter Three1
Three
Files
and
Directories
Date()Function parameters
Parameters Description
Format Required . Specifies the
format
Cont.
…
¡ The
required
format
parameter
in
the
date()
function
specifies
how
to
format
the
date/
time.
¡ d
-‐
Represents
the
day
of
the
month
(01
to
31)
Example:
<?php
echo
date(“Y/m/d”).”<br/>”;
//2019/04/15
echo
date(“Y.m.d”).”<br/>”;
//2019.04.15
echo
date(“Y-‐m-‐d”);
//2019-‐04-‐15
?>
Adding
a
Timestamp
¡ specifies
a
timestamp.
¡ the
current
date
and
time
will
be
used
as
a
default
timestamp.
Syntax
for
mktime()
mktime(hour,minute,second,month,day,year,is_dst)
¡ To
go
one
day
in
the
future
we
simply
add
one
to
the
day
argument
of
mktime().
Tuesday,
January
12,
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
2021
7
Example:
<?php
$tomorrow=mktime(0,0,0,date(“m”),date(“d”)+1,date(“Y”));
echo
“tomorrow
is:”.date(“Y/m/d”,$tomorrow);
//output:tomorrow
is
:2019/04/tomorrow’s
day
?>
¡ When
the
header
needs
to
be
updated,
you
can
only
update
the
include
file,
or
when
you
add
a
new
page
to
your
site,
you
can
simply
change
the
menu
file
(instead
of
updating
the
links
on
all
your
web
pages).
Tuesday,
January
12,
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
2021
10
header() function
Example:
header(“location:”)
$result
=
mysql_query
("INSERT
INTO
users
(id,
real_name,
username,
password,
email)
VALUES
('$ID',
'$real_name',
'$username',
'$userpass','$email')");
if(!$result)
{
echo
"<b>User
not
added:</b>
";
header("location:admin_home.php");
echo
"<b>User
not
added:</b>
",
mysql_error();
exit;
}
Tuesday,
January
12,
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
2021
12
<br/><br/>
</form>
Cont.
…
¡ By
using
the
global
PHP
$_FILES
array
you
can
upload
files
from
a
client
computer
to
the
remote
server.
¡ The
first
parameter
is
the
form's
input
name
and
the
second
index
can
be
either
"name",
"type",
"size",
"tmp_name"
or
"error".
Like
this:
Cont.
…
¡ $_FILES["file"]["name"]
-‐
the
name
of
the
uploaded
file
upload.php
file
<?php
if($_FILES["file"]["error"]>0)
{
echo
"Error:".$_FILES["file"]["error"];
}
else
{
echo
"Upload:".$_FILES["file"]["name"]."<br/>";
echo
"Type:".$_FILES["file"]["type"]."<br/>";
echo
"Size:".($_FILES["file"]["size"]/
1024)."Kb<br/>";
echo
"Stored
in:".$_FILES["file"]
["tmp_name"]."<br/>";
}
?>
Tuesday,
January
12,
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
2021
17
Note:
This
example
saves
the
file
to
a
new
folder
called
"upload"
Tuesday,
January
12,
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
2021
21
Opening a File
Modes Description
r Read only. Starts at the beginning of the file
r+ Read write. Starts at the beginning of the file
w Write only. Opens and clears the contents or creates a new file
if it doesn’t exist.
w+ Read/write. Opens and clears the contents or creates a new file
if it doesn’t exist.
a Append. Opens and writes to the end of the file or creates a
new file if it doesn’t exist.
a+ Read/append. Preserves file content by writing to the end of
the file
x Write only. Creates a new file. Returns FALSE and an error if file
already exists
x+ Read/write. Returns FALSE and an error if file already exists
Tuesday,
January
12,
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
2021
23
Example:
<html>
<body>
<?php
$file=fopen(“demo2.php”,”r”)
or
exit(“unable
to
open
file!”);
?>
</body>
</html>
Closing a File
Check
End-‐of-‐file
¡ The
feof()
function
checks
if
the
"end-‐of-‐
file"
(EOF)
has
been
reached.
Example
¡ The
example
below
reads
a
file
line
by
line,
until
the
end
of
file
is
reached:
<?php
$file=fopen(“text.txt”,”r”)
or
exit(“unable
to
open
file”);
while(!feof($file))
{
echo
fgets($file).”<br>”;
}
fclose($file);
?>
Tuesday,
January
12,
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
2021
31
Con’t
¡ Fread()
¡ fread()function
takes
a
filehandle
and
length
as
its
argument
and
return
a
specified
number
of
characters.
¡ Format
¡ $file=fopen("$filename”,"r");
¡ $contents=fread($file,5);
?>
¡ Format
$contents=file_get_contents("datafile.txt");
¡ Format :
$filename=”text.txt";
$filehandle=fopen($filename,"w");
fwrite($filehandle,
$outputstring,strlen($outputstring));
¡ The
file_put_contents()Function
¡ also
writes
a
string
to
a
file
and
returns
the
number
of
bytes
written,but
does
not
require
a
filehandle.
Otherwise
it
is
the
same
as
fwrite()
and
fputs().
$outputstring="$name\t$address\t$email\t$title\n";
$filename="info.txt";
$filehandle=fopen($filename,"a");
fwrite($filehandle,
$outputstring);
$text=
file_get_contents("$filename");
echo
"<pre>$text</pre>";
fclose($filehandle);
?>
Tuesday,
January
12,
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
2021
40
File
Checks
¡ Before
performing
operations
on
files
or
directories,
it
is
a
good
practice
to
verify
whether
or
not
the
file
even
exists,
if
it
is
readable,
writable,
executable,
and
so
on.
¡ PHP
provides
a
set
of
functions
for
testing
the
status
of
a
file.
cont
¡ is_file():
¡ Checks
if
the
filename
is
a
file.
¡ It
takes
the
name
of
the
file
as
its
argument
¡ returns
TRUE
if
the
file
is
a
regular
file,
and
FALSE
if
it
is
not.
Con’t
¡ is_file():
¡ Checks
if
the
file
name
is
a
file.
¡ It
takes
the
name
of
the
file
as
its
argument
and
returns
TRUE
if
the
file
is
a
regular
file,
and
FALSE
if
it
is
not.
Format
is_file
(string
filename)
Example:
Con’t
¡ is_readable()
:
¡ Checks
if
the
file
is
readable.
¡ It
takes
the
filename
as
its
argument
and
returns
TRUE
if
the
filename
exists
and
is
readable.
¡ Format
is_readable
(
string
filename
);
Example:
if
(
is_readable("file.txt"))
{
echo
"File
is
readable<br
/>”;
}
Tuesday,
January
12,
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
2021
45
Con’t
¡ is_writable():
¡ Checks
if
the
file
is
writable.
Format
is_writable
(
string
filename
)
Example:
if
(is_writable($filename))
{
echo
"$filename
is
writable";
}
¡ Format
¡ Format
rename(string
old_file,string
new_file)
Example:
¡ rename(“test.txt", “def.txt");
¡ Format
¡ Example:
unlink("datafile.txt");
Directories
¡ PHP
supports
a
number
of
functions
to
allow
you
to
work
with
directories
in
the
file
system.
¡ chroot():
¡ Changes
the
root
directory.
¡ closedir():
¡ Closes
a
directory
¡ Handle
previously
opened
with
opendir().
¡ getcwd()
¡ Gets
the
current
working
directory.
Tuesday,
January
12,
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
2021
53
¡ readdir():
¡ Reads
the
next
file
from
a
directory
handle
opened
with
opendir().
¡ rmdir():
¡ Deletes
a
directory.
¡ It
must
be
empty
and
have
write
permission.
Tuesday,
January
12,
Advanced
IP
Compiled
By:
Yonas
H.
(MSc.)
2021
54
¡ scandir():
¡ Returns
an
array
of
files
and
directories
from
a
given
path.
¡ unlink():
¡ Deletes
a
file
from
a
directory.
Example
<?php
$dir=opendir(“C:/xampp/htdocs/IS");
while($file=readdir($dir))
closedir($dirhandle);
?>