Exclude specific directories using PHP functions scandir _php Tutorial

Source: Internet
Author: User
The Scandir () function returns an array that contains the files and directories in the specified path. As shown below:

Example:

Copy the Code code as follows: Print_r (Scandir (' test_directory '));
?>
Output:

Copy the Code code as follows: Array
(
[0]=>.
[1]=>
[2]=>1.txt
[3]=>2.txt
)
In most cases, you only need an array of file lists for that directory, as follows:

Copy the Code code as follows: Array
(
[0]=>1.txt
[1]=>2.txt
)
Generally by excluding "." or ".." The array entries are resolved:

Copy the Code code as follows: functionfind_all_files ($dir)
{
$root = Scandir ($dir);
foreach ($rootas $value)
{
if ($value = = = '. ' | | $value = = = '.. ') {
Continue
}
if (Is_file ("$dir/$value")) {
$result [] = "$dir/$value";
Continue
}
foreach (Find_all_files ("$dir/$value") as$value)
{
$result [] = $value;
}
}
Return$result;
}
?>
Another method, using the Array_diff function, is to reject the array that the Scandir function performs:

Copy the Code code as follows: $directory = '/path/to/my/directory ';
$scanned _directory=array_diff (Scandir ($directory), Array ('.. ', '. '));
?>
Typically, code management generates an. svn file, or a. htaccess file that restricts directory access. Therefore, it is more convenient to filter by Array_diff function.

http://www.bkjia.com/PHPjc/788621.html www.bkjia.com true http://www.bkjia.com/PHPjc/788621.html techarticle the Scandir () function returns an array that contains the files and directories in the specified path. As follows: Example: Copy code code as follows: Php print_r (scandir (' test_directory ')); Lose ...

  • 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.