Working with directories in PHP

 Working with directories in PHP.

 Directory

  • ·       A directory is a file system.
  • ·       cataloging structure.
  • ·       Contains references to other computer files and other directories and subdirectories.
  • ·       Known as folders.

 Operations of directory:-   create, open, read, delete, list all files in the directory.

 

Creating a New Directory

 Use mkdir() function with the path and name of the directory.

 Syntax:-

mkdir($directory_path);

  • $directory_path: either relative and absolute path (location) if it wrong will return an error.

The function returns boolean data, that is, true on successful execution, false otherwise.

 Example:-

<?php // The directory path

$dir = "testdir"; // Check the existence of directory

if(!file_exists($dir))

{

// Attempt to create directory

if(mkdir($dir))

{

echo "Directory created successfully.";

}

Else

{

echo "ERROR: The directory could not be created."; }

}

else

{

echo "ERROR: Directory already exists.";

 }

 

?>

Output:-

Directory created successfully.

 

Closing Directory Link

To close this link after completing to the related functions

syntax:- 
closedir($directory_handle);

 

Reading Directory

  • ·       Returns the filenames in the order as they are stored in the directory.
  • ·       Use readdir() function.
  • ·       Returns the entry name/filename on success or false on failure.
  • ·       Use with loop.

 Syntax:

readdir(dir_handle)

parameters :-

dir_handle: a mandatory parameter,

specifies the handling resource previously opened by the opendir() function.

returns the entry name/filename on success, or false on failure.

 Example: -

<?php

  // opening a directory

$dir_handle = opendir("testdir ");

  // reading the contents of the directory

while(($file_name = readdir($dir_handle)) !== false) 

echo("File Name: " . $file_name);

echo "<br>"

}

 // closing the directory

closedir($dir_handle);

?>

Output:

File Name: .
File Name: ..
File Name: a1
File Name: a2
File Name: a3
File Name: a4
File Name: ab.txt.txt
File Name: abc.docx

 Removing Directory

rmdir() function use for removing directories .

similer to unlink().

 

Syntax:-

rmdir($directory_path);

 

Note :- function will remove the directory, if and only if it is empty.

For removing the non-empty directory, use unlink() function.

 Example :-

creating a new directory, open and read for listing directory content and closing directory .

<?php

mkdir("testdir2");

/*Creating files into php_directory_functions_manual*/

$file_pointer1 = fopen("testdir2/mkdir.txt","x");

$file_pointer2 = fopen("testdir2/rmdir.txt","x");

fclose($file_pointer1);

fclose($file_pointer2);

$directory_handle = opendir("testdir");

while($directory_item = readdir($directory_handle)) {

echo $directory_item . "<br>";

}

closedir($directory_handle);

?>

 Output:-

.
..
a1
a2
a3
a4
ab.txt.txt
abc.docx
mkdir.txt
rmdir.txt

 Example:- Remove directory

<?php

$directory_handle = opendir("testdir1");

while($directory_item = readdir($directory_handle)) {

@unlink("testdir1/".$directory_item);

   }

closedir($directory_handle);

$fp= rmdir("testdir1");

if(!file_exists($fp))

{

             echo("directory remove");

}

?>

 Output:-

directory remove

On iterating with a loop for reading each item stored into the directory, the unlink() function is invoked to wipe out the directory before attempting to delete it. And then, rmdir() is used by referring to the name of the directory, to remove it.

=============================================================


Post a Comment

0 Comments