Monday, 2 February 2015

PHP directry functions


PHP directry functions

FunctionsDescription
dirnameName of the directory from a sting input.

We can get directy name from the string we provide as input.
$var='http://www.plus2net.com/sql_tutorial/math.php';
echo dirname($var);
Output is here
http://www.plus2net.com/sql_tutorial
is_dirChecking if input string is directory Name or not.is_dir() function returns True or False to know if file name is directory or not. We will check by if condition.
$var='test_dir';
if(is_dir($var)){
echo "This is a directory : $var ";
}else{
echo "This is NOT a directory : $var ";
} 
 
scandirList of All files and directories present inside a directory.We can display all directory and files present inside a given directory by using scandir php function. This function returns an array. Here is the syntax.
Scandir('directory_sting',sort_order,'context');
Directy_sting: directory name as string
Sort_order: (Optional)The listing order is ascending by default ( alphabetical ). Can by changed by SCANDIR_SORT_DESCENDING and Random by SCANDIR_SORT_NONE
Context: ( Optional ) 

Here is the simple code to list all files and directory present in a directory.
$ar=scandir('test_dir'); // Change this input 
print_r($ar);
Listing all files and directory of current folder
$ar=scandir('.');  
print_r($ar);
Listing all files and directory of up folder
$ar=scandir('..'); 
print_r($ar);
We can develop a small script which will first list file and directory of current directory. All listed directory will have hyperlink to list files and directory present inside that on click. We have used is_dir function to check if the output string is a directory or not.
$var=$_GET['var'];
if(strlen($var)==0){
$var='.';
}
$ar=scandir($var);
while (list ($key, $val) = each ($ar)) {
if(is_dir($val)){
echo "<a href=scandir.php?var=$val>$val</a><br>";
}else {
echo "$val<br>";
}
}

Travelling to different subdirectory and up directory structure

Above script we will modify to provide option where users can click folders to visit those folders. They can move upward ( towards root ) or can move downward by travelling to different levels deep inside the folder structure. Here is the code to do that.

<?php
@$var=$_GET['var'];
$path = '';
if(strlen($var)==0){
$path='';
}else{
$path=$var;
}
$cwd=getcwd();
$cwd=$cwd.$path;

chdir($cwd);

echo "<br>Current Working Directory : ".getcwd()."<br>";

$ar=scandir($cwd);
while (list ($key, $val) = each ($ar)) {
$path2=$path."".$val;
if(is_dir($val)){
echo "<a href=dir-listing.php?var=$path2>$val</a><br>";
}else {
echo "$val<br>";
}
}

?>

Emptying folders by deleting all files

This is the best way to empty the files in a folder to delete the folder. This script will keep on deleting files as you move on different level of folders. 

Be warned that as you are going to delete the files not only of current folder but of all other folders you try to visit. 

We kept the delete command commented for safety reasons and after understanding all the consequences you can un comment the line and use it at your own risk.

<?php
@$var=$_GET['var'];
$path = '';
if(strlen($var)==0){
$path='';
}else{
$path=$var;
}
$cwd=getcwd();
$cwd=$cwd.$path;

chdir($cwd);

echo "<br>Current Working Directory : ".getcwd()."<br>";

$ar=scandir($cwd);
while (list ($key, $val) = each ($ar)) {
$path2=$path."".$val;
if(is_dir($val)){
echo "<a href=dir-listing.php?var=$path2>$val</a><br>";
}else {
if($val<>'dir-listing.php'){
// Uncommenting below line will delete your files of each folder you list ////
//unlink($val); // This will delete your files !!!!
}

echo "$val<br>";
}
}

?>

getcwdGet Current working directory details.Getcwd returns current directory name with path. This function returns true on successful and returns false if it failed to get any result. 
Here is a sample code.
echo getcwd();
The output is here in Linux server
/home/myuserid/public_html 
In a windows platform the output will be
J:\php_files\t\dir
chdirChange the current directry to new directory.chdir to change the directory to a different one. This function returns true on success and false on Failure. We have used getcwd() function to get the current directory location. Here is a sample code.
echo getcwd();
chdir('test_dir');
echo "<br>";
echo getcwd();
Output of above code is here
J:\php_files\t\dir
J:\php_files\t\dir\test_dir
To change from current directory to one step up directory
echo getcwd();
chdir('..');
echo "<br>";
echo getcwd();
As chdir() returns TRUE or FALSE we can use if condition to post message.
echo getcwd();
echo "<br>";
if(@chdir('test_dir2')){
echo getcwd();
}else{
echo " Wrong Directory ";
}
chrootChange to root directry from current directory.We can shift from current directory to root directory by using chroot function. This function will return true of false based on the outcome. You must have sufficient permission to execute this function. In windows platform this function will not work. So before using it is better to check this function. Here is an simple example.
echo getcwd(); // get the current location
chroot('H:\php_files\t\dir');
echo "<br>";
echo getcwd();
It is better to check the function by using function_exists. Here is the code
if ( function_exists("chroot") ){
chroot($chroot);
echo getcwd();
}else{
echo " this is not supported " ;
}
 

0 comments:

Post a Comment