chdir-Change directory
chroot-change the root directory
closedir- Close directory handle
dir-Return an instance of the directory class
getcwd-gets the current working directory
opendir-open directory handle
readdir-read entry from the directory handle.
rewinddir-Rewind directory handle
scandir-list files and directories inside the specified path.
chdir and getcwd:
See the below example
1 2 3 4 5 6 7 8 9 |
<? echo getcwd()."\n"; chdir('mydoubts'); echo getcwd()."\n"; ?> |
Above example will output like below
/home/vasanthan
/home/vasanthan/mydoubts
chroot-change the root directory. it will change the current directory to ‘/’
Example:
1 2 3 4 5 6 7 8 |
<? chroot("/data/vasanthan/mydoubts/"); echo getcwd(); ?> |
Ouput as below
/
Opendir-Open a directory handle
it returns directory handle resource on success ,or false on failure
Examples
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $dir = "/vasanthan/mydoubts/"; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { echo "filename: $file : filetype: " . filetype($dir . $file) . "\n"; } closedir($dh); } } ?> |
scandir-list files and directories inside the specified path.
Examples:
1 2 3 4 5 |
<?php $dir = 'C:/xampp/htdocs/test'; $files1 = scandir($dir); print_r($files1); ?> |
ouput will be like below
Array (
[0] => .
[1] => ..
[2] => index.php
)
tag:directory function in php,scandir example in php,opendir,chroot,getcwd
2. Sample script for reading csv file?
1 2 3 4 5 6 7 8 9 10 11 |
<? $myfile='hello.csv'; $filehandler=fopen($myfile,'r'); while(!feof($filehandler)) { $csvcontent[]=fgetcsv($filehandler,1024); } fclose($filehandler); ?> |
print_r($csvcontent);