PHP creating and deleting directories

PHP creating & deleting directories

Creating directories:

PHP’s mkdir() can be used to create directories. It takes two parameters; path to desired directory and the permission.
Mkdir(*/temp/sample*, 0777);

Deleting directories:

PHP’s rmdir() can be used to delete directories. It takes one parameter as the directory name to be deleted.
rmdir(*/temp/sample*);

Explain the working with directories using opendir(), readdirs(), closedir() along with examples.

Opendir():- It opens the directory. This function returns a directory stream on success and FALSE and an error on failure.

Syntax:
Opendir(directory, context)

Context is a set of options that can modify the behavior of a stream
Example: opens sample directory.
$dir = opendir("directory");

Readdir(): It returns an entry from a directory handle opened by opendir().

Syntax:
Readdir(dir_stream)

Example:
$file = readdir($dir);

closedir(): It closes a directory handle opened by opendir().

Syntax:
closedir(dir_stream)

Example:
$file = close($dir);

Write short note on creating directories, mkdir function with an example.

Mkdir(): creates a directory.

Syntax:
Mkdir(path,mode,recursive,context);

Mode: Optional. Specifies permissions. By default, the mode is 0777 (widest possible access).
The mode parameter consists of four numbers:

- The first number is always zero
- The second number specifies permissions for the owner
- The third number specifies permissions for the owner's user group
- The fourth number specifies permissions for everybody else

Possible values (to set multiple permissions, add up the following numbers):

- 1 = execute permissions
- 2 = write permissions
- 4 = read permissions

Recursive – optional and specifies if recursive
Context – optional and Specifies the context of the file handle.

Example:
Mkdir(“sample”);

Write short note on deleting directories, rmdir() with an example.


Rmdir() removes the directory which is empty.

Syntax:
Rmdir(dir, context);

Context: optional. Context is a set of options that can modify the behavior of a stream

Example:
<?php
$var = "images";
if(!rmdir($var))
{
       echo ("Could not remove $var");
}
?>
PHP creating, moving, copying and deleting files
PHP creating, moving, copying & deleting files - Copy files: Copies files from “source” to “destination”. If the destination file exists it will be overwritten. it returns true on success...
Post your comment

You may like