rmdir
REMOVES a particular directory.
This function attents to remove a particular directory, that must be empty, and with relevant permissions that must permit this.
An E_WARNING level error will be generated on failure.
It can be applied, even in symbolic links.
Returns TRUE on success or FALSE on failure.
<?php
bool rmdir ( str $directory , res $context = ? )
where,
$dirname = Path to the directory to be removed
$context = Context resource
( Since PHP 5.0.0 )
?>
$dirname
Path to the directory to be removed.
$context
A valid context resource criated by stream_context_create.
EXERCISE
<?php
$slk01 = 'symlk01';
$yn01 = @rmdir($slk01);
if($yn01 == true)
{
echo 'The ' . $slk01 . ' does not exists anymore!<br><br>';
}
else
{
echo 'The ' . $slk01 .
' still exists or was not yet created
or never existed.<br><br>';
}
?>
EXERCISE
<?php
$trg02 = __DIR__;
$lnk010 = 'symlk010';
// CREATES the symlk010
// REMEMBER to use this function on Windows
// you must run the server as Administrator
symlink($trg02, $lnk010);
sleep(10);
// This symlink must be removed
// once its use has been completed
// in this case, after 10 seconds
// REMOVES the symlk010
rmdir('symlk010');
/* - - - - - - - - - - - - - - - - - - - - - - - - - - -
If rmdir is used at the end of a code,
any symbolic link with the same name
is not available for use
in the rest of the code
To be available in the rest of the code,
only if it is recreated
However, if the same name is used
for an existing symbolic link, an
E_WARNING will be issued.
- - - - - - - - - - - - - - - - - - - - - - - - - - - */
?>
EXERCISE
<?php
$dir03 = __DIR__ . '/a3';
$ver03 = @rmdir($dir03);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If the directory does not exist,
an E_WARNING will be issued,
unless it inserts at the beginning
of the function name an @ character
$ver03 = @rmdir($dir03);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
if($ver03 == TRUE)
{
echo 'Existing directory has been
successfully deleted!<br><br>';
}
else
{
echo 'Directory could not be deleted.<br><br>';
}
?>
EXERCISE
<?php
$dir04 = 'file://' . __DIR__ . '/adds';
echo $dir04 . '<br><br>';
// $dir04, Directory must exist but is not empty
$ver04 = @rmdir($dir04);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If the directory is not empty,
another E_WARNING will be issued,
unless an @ character is entered
at the beginning of the function name
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
if($ver04 == TRUE)
{
echo 'Existing directory has been
successfully deleted!<br><br>';
}
else
{
echo 'Directory could not be deleted.<br><br>';
}
?>
RESULT
Directory could not be deleted.
Because it is not empty.
EXERCISE
<?php
$dir05 = __DIR__ . '/a5';
echo $dir05 . '<br><br>';
// $dir05, Directory does not exist
$ver05 = @rmdir($dir05);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If the directory does not exist,
another E_WARNING will be issued,
unless an @ character is entered
at the beginning of the function name
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
if($ver05 == TRUE)
{
echo 'Existing directory has been
successfully deleted!<br><br>';
}
else
{
echo 'Directory could not be deleted.<br><br>';
}
?>
RESULT
Directory could not be deleted.
Because does not exists.
EXERCISE
<?php
$dir06 = 'file://' . __DIR__ . '/adds/newdir/';
$opts6 = [ 'https' => [ 'method'=>"POST",
'header' => "Accept-language: en",
'content' => $dir06 ] ];
$cntxcr6 = stream_context_create($opts6);
@mkdir ($dir06, NULL, true, $cntxcr6);
sleep(15);
if(file_exists($dir06))
{
echo 'Directory created successfully!<br><br>';
}
else
{
echo 'Directory can not be created.<br><br>';
}
// Once fulfilled its purpose,
// can be erased, so it will be
// In this case, the directory will be
// deleted after 15 seconds
$ver06 = @rmdir($dir06);
if($ver06 == TRUE)
{
echo 'Existing directory has been
successfully deleted!<br><br>';
}
else
{
echo 'Directory could not be deleted.<br><br>';
}
?>