QUIZGUM

Coding Class

Quizgum : directory function

directory function

Function to open a directory : opendir()
Function to close a directory : closedir()
Function to read a directory : readdir()

The parameters of opendir and closedir are directory paths.

Let's look at an example.

/htdocs/dir.php

<?php
    echo "directory function example<br />";

    $dir = '/Applications/';
    //$dir = "c:/windows/";  //if you use windows OS

    if(is_dir($dir)){
        if($dirop = opendir($dir)){
            while(($filerd = readdir($dirop)) != false){
                echo " {$filerd} <br />";
            }
            echo "-------------------------------------- <br />";
        }
    } else {
        echo "{$dir} does not exist.<br />";
    }
    closedir($dirop);
?>

Result(for example MacOS)

php image

Result(for example windows)

php image php image

4line - Store the directory name in $dir.
7line - is_dir() is a function that determines whether a directory exists.
$dir is determined to be true and the following is done
8line - Use $dirop as a variable to open a directory.
$dirop = Opens a directory in opendir($dir) and outputs the folders and files in it while looping while(($filerd = readdir($dirop))! = false).

Finding and relocating directories getcwd(0, chdir()

Use getcwd() to find out where the current directory is, and then use chdir() to specify the path of the directory to change.

/htdocs/getcwd.php

<?php
    echo "getcwd() , chdir() <br />";
    echo "current location <br />";

    $path = getcwd();

    echo "{$path} <br />";
    chdir("/Applications/");
    //chdir("c:/temp/"); //if you use windows OS

    $path = getcwd();

    echo "current location <br />";
    echo " $path <br />";
?>
php image