Download as pdf or txt
Download as pdf or txt
You are on page 1of 61

Chapter

 Three  
Files  and  Directories    

Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  


2  

The  PHP  Date()  Function  


¡ The  PHP  date()  function  formats  a  
timestamp  to  a  more  readable  date  
and  time.  
¡ A  timestamp  is  a  sequence  of  
characters,  denoting  the  date  and/or  
time  at  which  a  certain  event  
occurred.    
Syntax  
¡ date(format,timestamp)  
Tuesday,  January  12,  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
3  

Date()Function  parameters    

Parameters Description
Format Required . Specifies the
format

Timestamp Optional. Specifies a


timestamp .Default is the
current date and time

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
4  

Cont.  …  
¡ The  required  format  parameter  in  the  date()  
function  specifies  how  to  format  the  date/
time.  

Here  are  some  characters  that  can  be  used:  

¡ d  -­‐  Represents  the  day  of  the  month  (01  to  
31)    

¡ m  -­‐  Represents  a  month  (01  to  12)    

¡ Y  -­‐  Represents  a  year  (in  four  digits)    

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
5  

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  

?>  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
6  

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    

?>  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
8  

PHP  Include  File  


¡ You  can  insert  the  content  of  one  PHP  file  
into  another  PHP  file  before  the  server  
executes  it,  with  the  include()  or  
require()  function.  

¡ The  two  functions  are  identical  in  every  


way,  except  how  they  handle  errors:  
¡ include()  generates  a  warning,  but  the  
script  will  continue  execution    
¡ require()  generates  a  fatal  error,  and  the  
script  will  stop    

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
9  

PHP  Include  File  


¡  These  two  functions  are  used  to  create  
functions,  headers,  footers,  or  elements  that  
will  be  reused  on  multiple  pages.  
¡  Server  side  includes  saves  a  lot  of  work.    

¡  This  means  that  you  can  create  a  standard  


header,  footer,  or  menu  file  for  all  your  web  
pages.    

¡  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    

¡ Page  redirecting  function    

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
11  

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  

PHP  File  Upload  

¡ Create  an  Upload-­‐File  Form  


¡ To  allow  users  to  upload  files  
from  a  form  can  be  very  useful.    

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
13  

PHP  File  Upload  


<form  action="upload.php"  method="POST”  
enctype=“multipart/form-­‐data”>  
     <lable  for="file">Filename:</lable>  

       <input  type="file"  name="upload">  

                 <br/><br/>  

         <input  type="submit"  name="submit1">  

</form>  

/*The  enctype  attribute  of  the  <form>  tag  


specifies  which  content-­‐type  to  use  when  
submitting  the  form. */
Tuesday,  January  12,  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
14  

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:  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
15  

Cont.  …  
¡  $_FILES["file"]["name"]  -­‐  the  name  of  the  
uploaded  file    

¡  $_FILES["file"]["type"]  -­‐  the  type  of  the  


uploaded  file    

¡  $_FILES["file"]["size"]  -­‐  the  size  in  bytes  


of  the  uploaded  file    

¡  $_FILES["file"]["tmp_name"]  -­‐  the  name  of  the  


temporary  copy  of  the  file  stored  on  the  
server    
¡  $_FILES["file"]["error"]  -­‐  the  error  code  
resulting  from  the  file  upload    
Tuesday,  January  12,  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
16  

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  

Restrictions  on  Upload  


¡ In  this  script  we  add  some  
restrictions  to  the  file  upload.    
¡ The  user  may  only  upload  .gif  
or  .jpeg  files  and  the  file  size  
must  be  under  20  kb:  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
18  

Restrictions  on  Upload  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
19  

Saving  the  Uploaded  File  


¡ The  examples  above  create  a  
temporary  copy  of  the  uploaded  files  
in  the  PHP  temp  folder  on  the  
server.  
¡ The  temporary  copied  files  
disappears  when  the  script  ends.    
¡ To  store  the  uploaded  file  we  need  
to  copy  it  to  a  different  location:  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
20  

Note:  This  example  saves  the  file  to  a  new  folder  called  
"upload"  
Tuesday,  January  12,  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
21  

PHP  File  Handling  


¡ The  fopen()  function  is  used  to  open  files  
in  PHP.  

Opening  a  File  

¡ The  fopen()  function  is  used  to  open  files  


in  PHP.  

¡ The  first  parameter  of  this  function  


contains  the  name  of  the  file  to  be  opened  
and  the  second  parameter  specifies  in  which  
mode  the  file  should  be  opened:  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
22  

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>  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
24  

Some  Useful  File    


Functions  
¡ fclose():Closes  a  file  or  URL                  
       Eg.fclose(fh);    
¡ feof():Tests  for  end-­‐-­‐of-­‐-­‐file      
       Eg.  feof(fh);    
¡ fgetc():Gets  a  character  from  a  
file.    
       Eg.  fgetc(fh);  
¡ fgets():  Gets  a  line  from  a  file    
       Eg.  fgets(fh);  
Tuesday,  January  12,  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
25  

Opening  a  File  for  Reading  


¡  B e f o r e   o p e n i n g   a   f i l e   y o u   n e e d   t o  
determine   whether   or   not   the   file   exists  
and  is  accessible  for  reading.    

¡  The   file_exists()   function   checks   to   see  


if   the   file   exists,   and   the   is_readable()  
function  will  return  true  if  a  file  exists  
and  has  read  permission.  

¡ Four  PHP  functions  are  used  for  reading  


text  from  a  file:fgets(),  fgetc(),  fread(),  
and  file_get_contents().  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
<?php   26  
$filename=“text.txt";  
$fh=fopen("$filename”,"r");  
if  (!file_exists($filename))  
{  //  Check  for  file  existence  
       print  "No  such  file  or  directory";  
     exit();  }  
else {
while(!feof($fh))
{
$content= fgets($fh);
echo $content;
} fclose($fh); }
?>  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
27  

Closing  a  File  

¡ The  fclose()  function  is  used  to  


close  an  open  file:  
<?php  
$file=fopen(“demo2.php”,”r”)  or  
exit(“unable  to  open  file!”);    
fclose($file);  
?>  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
28  

Check  End-­‐of-­‐file  
¡ The  feof()  function  checks  if  the  "end-­‐of-­‐
file"  (EOF)  has  been  reached.  

¡ The  feof()  function  is  useful  for  looping  


through  data  of  unknown  length.  

¡ Note:  You  cannot  read  from  files  opened  in  


w,  a,  and  x  mode  

¡ Syntax:  if(feof($file))echo  “end  of  file”;  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
29  

Reading  a  File  Line  by  Line  

¡ The  fgets()  function  is  used  to  read  


a  single  line  from  a  file.  
¡ Note:  After  a  call  to  this  function  
the  file  pointer  has  moved  to  the  
next  line.    

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
30  

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  

Reading  a  File  Character  by  


Character  

¡ The  fgetc()  function  is  used  to  read  


a  single  character  from  a  file.  
¡ Note:  After  a  call  to  this  function  
the  file  pointer  moves  to  the  next  
character.    

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
32  

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

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
33  

Reading  a  File  Character  by  


Character  
¡ The  example  below  reads  a  file  character  by  
character,  until  the  end  of  file  is  
reached:  
<?php  
$file=fopen(“text.txt”,”r”)  or  exit(“unable  to  
open  file”);  
while(!feof($file))  
{  
echo  fgetc($file).”<br>”;  
}  
fclose($file);  

?>  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
34  

Reading  from  Files  Without  a  


Filehandle  
¡ PHP  provides  functions  that  allow  you  to  
read  the  contents  of  a  file  without  first  
opening  a  filehandle.  
¡ file_get_contents():  
¡ Reads  and  returns  entire  file  in  a  string.  

¡ Format  
$contents=file_get_contents("datafile.txt");  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
35  

Opening  a  File  for  Writing  


and  Appending  
The  fwrite()  and  fputs()  Functions  
¡  The  fwrite()  function  writes  a  string  text  to  a  
file  .  

¡  It  takes  three  arguments:    


¡  the  filehandle  returned  by  fopen()  ,  
¡  a  string  that  will  write  to  the  file  
¡  an  optional  length  argument(how  many  bytes  to  
write  to  the  file.)    

¡  Format  :  

 fwrite  (filehandle,string,[int  length]);  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
36  
<?php  
$name="Aster";  
$address="AA";  
$email="ate234@gmail.com";  
$title="Student";  
$outputstring="$name\t$address\t$email\t$title\n";  

$filename=”text.txt";  
$filehandle=fopen($filename,"w");  
fwrite($filehandle,  
$outputstring,strlen($outputstring));  

echo  "Writing  data  successfully";  


fclose($filehandle);  
?>  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
37  

Opening  a  File  for  Writing  

¡ 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().  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
38  

Opening  a  File  for  Appending  


¡ When  a  file  is  opened  for  
appending,  the  write  will  start  
at  the  end  of  the  file.    
¡   If  the  file  does  not  exist,  it  
will  be  created.  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
<?php  
39  
$name="Aster";  
$address="AA";  
$email="ate234@gmail.com";  
$title="Student";  

$outputstring="$name\t$address\t$email\t$title\n";  
$filename="info.txt";  
$filehandle=fopen($filename,"a");  
fwrite($filehandle,  $outputstring);  

echo  "Data  added  sucssfully";  

         $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.  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
41  

File  Testing  Functions  


¡ file_exists():  
¡ Checks  if  the  file  or        directory  exists.    
¡ It  returns  TRUE  if  it  does,  and  FALSE  if  it  
does  not.  
¡ Format  
           file_exists  (  string  filename  );  
¡ Example:  
     if  (  file_exists(“demo.php");  
¡ is_dir():Checks   if   the   filename   is   a  
directory.  
Tuesday,  January  12,  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
42  

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.  

¡ Format:    is_file  (  string  filename  )  


¡ Example:    if  (  is_file(”demo.php")  
{ print "True"; }

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
43  

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:  

   if  (  is_file("myfile.php"){  print  "True”;}  


Tuesday,  January  12,  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
44  

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";  
     }  

¡ stat():gives  information  about  the  file.  


Tuesday,  January  12,  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
46  
Creating,  Copying,  
Renaming,  and  Deleting  
Files  
•  Functions  to  Manipulate  Files  
¡ copy()        Copies    a  file.    
¡ rename()  Renames    a  file.  
¡ unlink()  Removes    a  file.  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
47  

The  copy()  Function:  


Making  a  Copy  of  a  File  
¡  The  copy()  function  will  return  true  if  the  file  
was  correctly  copied,  or  false  if  there  was  an  
error.    

¡  To  copy  a  file,  you  will  need  write  permission  on  


the  directory  where  the  new  copy  will  be  stored.  

¡  Format    

 bool  copy(string  source_file,string  


destination_file)  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
48  

The  copy()  Function:  


Example    
<?php  
$oldfilename="info.txt  ";  
$newfilename="test.txt";  
if(copy($oldfilename,$newfilename))  
       {  
   echo  "Copy  succeeded!\n<br  />”;  exit();  }  
else    
{  
echo  "Copy  failed\n<br  />";    }  
?>  
Tuesday,  January  12,  
Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
49  

The  rename()  Function:  


Renaming  and/or  Moving  a  File  
¡ The  rename()  function  is  used  to  give  a  
file  or  directory  another  name.  

¡ It  returns  true  on  success,  and  false  if  it  


fails.  

¡ Format    
rename(string  old_file,string  new_file)          

Example:  

¡ rename(“test.txt",  “def.txt");  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
50  

The  unlink()  Function:  


Removing  a  File  
¡ The  unlink()  function  is  used  to  remove  a  
file.  It  returns  true  if  it  can  remove  the  
file  and  false  if  not.  

¡ Format    

               unlink  (  string  filename  )          

¡ Example:  

               unlink("datafile.txt");    

//  Deletes  the  file  from  the  directory  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
51  

Directories  
¡  PHP  supports  a  number  of  functions  to  
allow  you  to  work  with  directories  in  the  
file  system.    

¡ From  a  PHP  script,  you  can  open  a  directory  


and  read  its  contents  ,you  can  change  to  a  
new  directory,  list  the  current  working  
directory,  remove  a  directory,  and  so  on.    

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
52  

PHP  Directory  Functions    


¡ chdir():  
¡ Changes  the  directory.      

¡ 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  

PHP  Directory  Functions    


¡ opendir():  
¡ Returns  a  directory  handle  that  can  be  
used  with  readdir(),closedir().  

¡ 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  

PHP  Directory  Functions    

¡ scandir():  
¡ Returns  an  array  of  files  and  
directories    from  a  given  path.  
¡ unlink():  
¡ Deletes  a  file  from  a  directory.  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
55  

Opening  and  Reading  from  


a  Directory  
¡ When  you  open  a  directory  with  the  
opendir()  function,  you  create  a  
directory  handle  to  allow  access  to  
the  directory  as  it  is  stored  on  the  
disk  by  the  operating  system  
regardless  of  its  internal  
structure.    
¡ Once  it  is  opened,  you  can  access  
the  directory  with  the  PHP  functions    

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
56  

Opening  and  Reading  from  


a  Directory  
¡ The  opendir()  Function  
¡ The  opendir()  function  is  used  to  open  
a  directory,  similar  to  the  fopen()  
function  for  opening  files.  
¡ Once  a  handle  to  the  directory  is  
returned,  the  other  directory  
functions,  such  as  readdir(),  
rewindir(),and  closedir(),  can  be  
applied  to  the  directory  filehandle.  
¡   If  the  directory  cannot  be  opened,  
false  will  be  returned.    

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
57  

Opening  and  Reading  from  a  


Directory  
¡ Format  
opendir(string  path)  
¡ Example:  
//$dirhandle  is  a  resource  similar  to  a  filehandle  
$dirhandle=opendir("/home/john");  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
58  

The  readdir()  Function  


¡ A  directory  can  be  read  by  anyone  who  has  
read  permission  on  the  directory.    

¡ When  we  speak  of  reading  a  directory,  we  


are  talking  about  looking  at  its  contents  
with  the  readdir()  function.  

¡ readdir()  reads  an  entry  from  a  directory  


handle,  given  as  its  argument,  and  returns  
the  name  of  a  file  from  the  directory.  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
59  

The  readdir()  Function  


¡ Format  
readdir  (resource  dir_handle  )          
¡ Example:  
 $dirhandle=opendir("/home");    
//Gets  one  file  from  the  directory.    
 //Use  a  loop  to  get  them  all  
one_file=readdir($dirhandle);

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
60  

Example  
<?php  

       $dir=opendir(“C:/xampp/htdocs/IS");  

         //List  all  files  in  the  directory  

     while($file=readdir($dir))  

         {    echo  $file,"<br  />”;    }  

       closedir($dirhandle);  

?>  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  
61  

Tuesday,  January  12,  


Advanced  IP                                                                    Compiled  By:  Yonas  H.  (MSc.)                  
2021  

You might also like