PHP opendir () Readdir () Scandir ()

Source: Internet
Author: User

If the Opendir (Path,context) succeeds, the function returns a directory stream, otherwise it returns false and an error. You can hide the output of an error by adding "@" in front of the function name.

The Readdir () function returns an entry in a table of contents handle opened by Opendir (). If successful, the function returns a filename, otherwise it returns false.

The Scandir () function returns an array that contains the files and directories in the specified path.
If successful, returns an array that, if it fails, returns FALSE. If directory is not a directory, returns a Boolean value false

To pay the 2-paragraph read directory code, the pro-test effective

Displays the file name in the directory

Open the directory and read its content
if (Is_dir ($dir)) {
  if ($dh = Opendir ($dir)) {while
    ($file = Readdir ($DH))!== false) {
      echo "FileName:". $file. "<br>";
    }
    Closedir ($DH);
  }

//Copy a directory file to another directory

copy_dir ($from _dir, $to _dir);
    function Copy_dir ($from _dir, $to _dir) {if (!is_dir ($from _dir)) {return false;
    echo "\ r \ n from:", $from _dir, '---to ', $to _dir;
    $from _files = Scandir ($from _dir);   
    If the destination directory does not exist, attempt to create an if (!file_exists ($to _dir)) {@mkdir ($to _dir); } if (!empty ($from _files)) {foreach ($from _files as $file) {if ($file = = '. ' | | $file = = ' ... ')
            {continue; if (Is_dir ($from _dir. $file)) {//If it is a directory, call itself Copy_dir ($from _dir. '/'. $file, $to _dir. '/'. $fil
            e);
            }else{//direct copy to target folder copy ($from _dir. '/'. $file, $to _dir. '/'. $file); }
        }
    }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.