Home >>Advance PHP Tutorial >PHP Directory

PHP Directory

Work With Directory PHP

Directory is the collection of related files.

How to create a directory using PHP

Syntax
<?php

  mkdir("your_dir_name");

?>
Eg
<?php

         mkdir("mydocs");

?>
In the above example in this program we use mkdir()function . Pass the directory name inside this function to create the directory.

Create a sub directory inside existing directory

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

?>

Eg
<?php

mkdir("mydocs/new updated docs");

?>

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
<?php
     
rmdir("mydocs");

?>
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
<?php

rename("mydocs","my updated docs");

?>
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
<?php


echo  file_exists("mydocs");

?>
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
<?php

$files =  scandir("mydocs");

print_r($files);

?>

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
<?php

$od =  openddir("mydocs");

?>

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
<?php

$od =  opendir("mydocs");

while($files =  readdir("mydocs"))

{

echo $files."<br/>";

}

?>

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.

No Sidebar ads