03-Apr-2023
.
Admin
Hi guys,
Today i will explained how to use php directory functions. This example is so easy to use in php.
This functions is provided to the default php language. So you can easily to use in your php code. Today i will explaind chdir(),chroot(),closedir(),dir(),getcwd() to explainded.
So let's start to the example.
chdir() Function
The chdir() function changes the current directory.
<?php
echo getcwd() . "<br>";
// Change directory
chdir("images");
echo getcwd();
?>
Output
/home/php
/home/php/images
chroot() Function
The chroot() function changes the root directory of the current process to directory, and changes the current working directory to "/".
Note: This function is not implemented on Windows platforms
<?php
chroot("/path/to/chroot/");
echo getcwd();
?>
Output
/
closedir() Function
closedir() function closes a directory handling.
<?php
directory = "/images/";
if (is_dir(directory)){
if ($dh = opendir(directory)){
while (($file = readdir($dh)) !== false){
echo "filename:" . $file . "<br>";
}
closedir($dh);
}
}
?>
Output
filename: cat.gif
filename: dog.gif
dir() Function
The dir() function returns an instance of the Directory class. This function is used to read a directory.
*The given directory is opened
*The two properties handle and path of dir() are available
*Both handle and path properties have three methods: read(), rewind(), and close()
<?php
$d = dir(getcwd());
echo "Handle: " . $d->handle . "<br>";
echo "Path: " . $d->path . "<br>";
while (($file = $d->read()) !== false){
echo "filename: " . $file . "<br>";
}
$d->close();
?>
Output
Handle: Resource id #2
Path: /etc/php
filename: .
filename: ..
filename: ajax.gif
filename: books.xml
filename: cdcatalog.xml
filename: cd_catalog.xml
filename: default.asp
filename: demo_array.asp
filename: demo_array.htm
getcwd() Function
getcwd() function returns the current working directory
<?php
echo getcwd();
?>
Output
/home/php
Now you can check your own.
I hope it can help you...
#PHP 8
#PHP