Home php教程 php手册 使用PHP函数scandir排除特定目录

使用PHP函数scandir排除特定目录

Jun 13, 2016 am 09:33 AM
php function Table of contents

scandir()函数返回一个数组,其中包含指定路径中的文件和目录。如下所示:

例子:

复制代码 代码如下:

print_r(scandir('test_directory'));
?>


输出:

复制代码 代码如下:

Array
(
[0]=>.
[1]=>..
[2]=>1.txt
[3]=>2.txt
)


大部分情况只需要该目录的文件列表数组,如下:

复制代码 代码如下:

Array
(
[0]=>1.txt
[1]=>2.txt
)


一般是通过排除“.”或者“..”的数组项解决的:

复制代码 代码如下:

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;
    }
?>


另外一种方法,利用array_diff函数,剔除scandir函数执行得到的数组:

复制代码 代码如下:

$directory='/path/to/my/directory';
$scanned_directory=array_diff(scandir($directory),array('..','.'));
?>


通常情况代码管理会产生.svn文件,或者限制目录访问权限的.htaccess等文件。所以通过array_diff函数来过滤会更方便。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The glob() function in PHP is used to find files or directories The glob() function in PHP is used to find files or directories Nov 18, 2023 pm 06:17 PM

The glob() function in PHP is used to find files or directories and is a powerful file operation function. It can return the path of a file or directory based on a specified pattern match. The syntax of the glob() function is as follows: glob(pattern, flags) where pattern represents the pattern string to be matched, which can be a wildcard expression, such as *.txt (matching files ending with .txt), or a specific file path. flags is an optional parameter used to control the function

How to read the catalog when reading on WeChat How to view the catalog How to read the catalog when reading on WeChat How to view the catalog Mar 30, 2024 pm 05:56 PM

The mobile version of WeChat Reading App is a very good reading software. This software provides a lot of books. You can read them anytime, anywhere with just one click to search and read them online. All of them are officially authorized and different types of books are neatly arranged. Sort and enjoy a comfortable and relaxing reading atmosphere. Switch the reading modes of different scenarios, update the latest book chapters continuously every day, support online login from multiple devices, and batch download to the bookshelf. You can read it with or without the Internet, so that everyone can discover more knowledge from it. Now the editor details it online Promote the method of viewing the catalog for WeChat reading partners. 1. Open the book you want to view the catalog and click in the middle of the book. 2. Click the three lines icon in the lower left corner. 3. In the pop-up window, view the book catalog

How to automatically generate a directory. How to set the format of the automatically generated directory. How to automatically generate a directory. How to set the format of the automatically generated directory. Feb 22, 2024 pm 03:30 PM

Select the style of the catalog in Word, and it will be automatically generated after the operation is completed. Analysis 1. Go to Word on your computer and click to import. 2After entering, click on the file directory. 3 Then select the style of the directory. 4. After the operation is completed, you can see that the file directory is automatically generated. Supplement: The table of contents of the summary/notes article is automatically generated, including first-level headings, second-level headings and third-level headings, usually no more than third-level headings.

How performant are PHP functions? How performant are PHP functions? Apr 18, 2024 pm 06:45 PM

The performance of different PHP functions is crucial to application efficiency. Functions with better performance include echo and print, while functions such as str_replace, array_merge, and file_get_contents have slower performance. For example, the str_replace function is used to replace strings and has moderate performance, while the sprintf function is used to format strings. Performance analysis shows that it only takes 0.05 milliseconds to execute one example, proving that the function performs well. Therefore, using functions wisely can lead to faster and more efficient applications.

Summary of methods for implementing image editing and processing functions using PHP image processing functions Summary of methods for implementing image editing and processing functions using PHP image processing functions Nov 20, 2023 pm 12:31 PM

PHP image processing functions are a set of functions specifically used to process and edit images. They provide developers with rich image processing functions. Through these functions, developers can implement operations such as cropping, scaling, rotating, and adding watermarks to images to meet different image processing needs. First, I will introduce how to use PHP image processing functions to achieve image cropping function. PHP provides the imagecrop() function, which can be used to crop images. By passing the coordinates and size of the cropping area, we can crop the image

Use tree to generate a file directory tree for display Use tree to generate a file directory tree for display Mar 01, 2024 pm 05:46 PM

tree is a command line tool that recursively lists the contents of a directory in a tree format, so that all directories, subdirectories, and files are listed in a hierarchical manner, thereby visually displaying the organizational structure of files and folders. The following are the installation and use methods of tree under Windows and Linux systems. The installation and use of tree under Linux. Installing tree under Linux: aptupdate&&aptinstalltree The following are the common ways of using the tree command. #Display the directory tree under the specified path tree/d/temp #Limit the maximum display depth tree-L3 #Display only directories but not files tree-d #Display including hidden files and directories tr

Comparing PHP functions to functions in other languages Comparing PHP functions to functions in other languages Apr 10, 2024 am 10:03 AM

PHP functions have similarities with functions in other languages, but also have some unique features. Syntactically, PHP functions are declared with function, JavaScript is declared with function, and Python is declared with def. In terms of parameters and return values, PHP functions accept parameters and return a value. JavaScript and Python also have similar functions, but the syntax is different. In terms of scope, functions in PHP, JavaScript and Python all have global or local scope. Global functions can be accessed from anywhere, and local functions can only be accessed within their declaration scope.

Similarities and differences between PHP functions and Flutter functions Similarities and differences between PHP functions and Flutter functions Apr 24, 2024 pm 01:12 PM

The main differences between PHP and Flutter functions are declaration, syntax and return type. PHP functions use implicit return type conversion, while Flutter functions explicitly specify return types; PHP functions can specify optional parameters through ?, while Flutter functions use required and [] to specify required and optional parameters; PHP functions use = to pass naming Parameters, while Flutter functions use {} to specify named parameters.

See all articles