3/31/15

PHP Directory

Directory is the collection of related files.

How to create a directory using PHP

Syntax
  <?php
         mkdir("your_dir_name");
  ?>
Eg i
  <?php
         mkdir("mydocs");
  ?>
   Output :
In the above example
in this program we use mkdir()function . Pass the directory name inside this function to create the directory.

How to create a sub directory inside a directory using PHP

Syntax
  <?php
         mkdir("your_dir_name/your_sub_dir_name");
  ?>

Eg ii
  <?php
         mkdir("mydocs/new updated docs");
  ?>
   Output :
In the above example use mkdir( ) function . pass the directory name/sub directory name inside mkdir() function sub directory create inside the main directory(make sure first must create a directory).

How to remove(delete) a directory in PHP
Syntax
  <?php
         rmdir("your_dir_name");
  ?>

Eg iii
  <?php
         rmdir("mydocs");
  ?>
   Output :
In the above example
We want to delete existing directory. Pass directory name “mydocs” inside rmdir( ) function.
it will delete the directory.
How to rename a directory
Syntax
  <?php
         rename("your_old_dir_name","new _ dir _name");
  ?>

Eg iv
  <?php
         rename("mydocs","my updated docs");
  ?>
   Output :
In the above example
if we want to rename a directory name. rename( ) function is used. it accept two argument first “the existing directory name” and second “new name which replace first directory name”.
How to check existence of directory
Syntax
  <?php
        echo file_exists("your_dir_name"");
  ?>

Eg v
  <?php
    echo  file_exists("mydocs");
  ?>
   Output :
In the above example
if we want to check the existence of a directory. file_exist( ) function is used with (directory name) .
if directory exist it gives true(“1″)

How to get files(contents) from directory
Note first manually store some files inside your directory
Syntax
  <?php
        scandir("your_dir_name"");
  ?>

Eg vi
  <?php
    $files =  scandir("mydocs");
    print_r($files);
  ?>
   Output :
in the above example
We get the contents of a directory. use scandir( ) function , directory name declare inside this.
scandir( ) function returns the files in array so stored the return value in a variable( $files).
Now print this using print_r($files) function i.e specially used to print the value and index of array.
it gives an output of an array type with index and their corresponding value.
How to open a directory
Syntax
  <?php
        opendir("your_dir_name"");
  ?>

Eg vii
  <?php
    $od =  openddir("mydocs");

  ?>
   Output :
In the above example
if we open the directory use opendir( ) function with directory name (“mydocs”).
store in variable $files because these open directory variable is going to used in further communication(for reading the contents).

How to read all files from a directory
Syntax
  <?php
        $files = readdir($od);
  ?>

Eg viii
  <?php
    $od =  opendir("mydocs");
    while($files =  readdir("mydocs"))
     {
       echo $files."<br/>";
    }
  ?>
   Output :
In the above example
first we open the directory of name(“mydocs”) and stores the values in $files(in previous example).
Now start to read the file using readdir( ) function till the file ends because here we are using while( ) loop.
to display the file name we have used echo statement in while loop.

0 comments:

Post a Comment

FIND US ON FACEBOOK

FIND US ON Twitter