Home Backend Development PHP Tutorial PHP iterate through directories and save

PHP iterate through directories and save

Jul 29, 2016 am 09:11 AM
arr array dir path

递归方式:

<code><span><span>function</span><span>getfiles</span><span>(<span>$path</span>,<span>$arr</span> = array<span>()</span>)</span>{</span><span>foreach</span>(scandir(<span>$path</span>) <span>as</span><span>$afile</span>){                           
        <span>if</span>(<span>$afile</span>==<span>'.'</span>||<span>$afile</span>==<span>'..'</span>) <span>continue</span>; 
        <span>if</span>(is_dir(<span>$path</span>.<span>'/'</span>.<span>$afile</span>)) {                       
            <span>$tmp</span> = getfiles(<span>$path</span>.<span>'/'</span>.<span>$afile</span>,<span>$arr</span>);
            <span>$arr</span> += <span>$tmp</span>;
        } <span>else</span> {
            <span>$key</span> = substr(<span>$afile</span>,strlen(<span>'hotdict_json_old_'</span>),<span>6</span>);
            <span>if</span>(<span>empty</span>(<span>$arr</span>[<span>$key</span>])){
                <span>$arr</span>[<span>$key</span>][] = <span>$afile</span>;
            }<span>else</span>{  
                array_push(<span>$arr</span>,<span>$afile</span>);
            }
        }
    }       
    <span>return</span><span>$arr</span>;
} </code>
Copy after login

循环方式:

<code><span><span>function</span><span>scanfiles</span><span>(<span>$dir</span>)</span> {</span><span>if</span> (! is_dir ( <span>$dir</span> ))
        <span>return</span><span>array</span>();
    <span>$dir</span> = rtrim(str_replace(<span>'\\'</span>,<span>'/'</span>,<span>$dir</span>),<span>'/'</span>).<span>'/'</span>;
    <span>$dirs</span> = <span>array</span>(<span>$dir</span>);
    <span>$rt</span> = <span>array</span>();
    <span>do</span> {
        <span>$dir</span> = array_pop(<span>$dirs</span>);
        <span>$tmp</span> = scandir(<span>$dir</span>);
        <span>foreach</span> ( <span>$tmp</span><span>as</span><span>$f</span> ) {
            <span>if</span> (<span>$f</span> == <span>'.'</span> || <span>$f</span> == <span>'..'</span>)
                <span>continue</span>;
            <span>$path</span> = <span>$dir</span>.<span>$f</span>;
            <span>if</span> (is_dir(<span>$path</span>)){
                array_push(<span>$dirs</span>,<span>$path</span>.<span>'/'</span>);
                <span>$rt</span>[<span>$f</span>] = <span>array</span>();
            }<span>else</span><span>if</span>(is_file(<span>$path</span>)){ 
                <span>if</span>(!strstr(<span>$f</span>,<span>'alpha'</span>)){
                    <span>if</span>(strstr(<span>$f</span>,<span>'all'</span>)){
                        <span>$key</span> = substr(<span>$f</span>,strlen(<span>'hotdict_all_json_old_'</span>),<span>6</span>);
                    }<span>else</span>{
                        <span>$key</span> = substr(<span>$f</span>,strlen(<span>'hotdict_json_old_'</span>),<span>6</span>);
                    }
                    array_push(<span>$rt</span>[<span>$key</span>],<span>$f</span>);
                }
            }
        }
    } <span>while</span>(<span>$dirs</span>); 
    <span>return</span><span>$rt</span>;
}</code>
Copy after login

以上代码有些逻辑是为了解决实际问题,请忽略,看懂代码即可!

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

版权声明:转载请保留文章署名和链接

以上就介绍了PHP遍历目录并保存,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Steps to set the PATH environment variable of the Linux system Steps to set the PATH environment variable of the Linux system Feb 18, 2024 pm 05:40 PM

How to set the PATH environment variable in Linux systems In Linux systems, the PATH environment variable is used to specify the path where the system searches for executable files on the command line. Correctly setting the PATH environment variable allows us to execute system commands and custom commands at any location. This article will introduce how to set the PATH environment variable in a Linux system and provide detailed code examples. View the current PATH environment variable. Execute the following command in the terminal to view the current PATH environment variable: echo$P

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

How to set the path environment variable How to set the path environment variable Sep 04, 2023 am 11:53 AM

Method to set the path environment variable: 1. Windows system, open "System Properties", click the "Properties" option, click "Advanced System Settings", in the "System Properties" window, select the "Advanced" tab, and then click "Environment Variables" " button, find and click "Path" to edit and save; 2. For Linux systems, open the terminal, open your bash configuration file, add "export PATH=$PATH: file path" at the end of the file and save it; 3. For MacOS system, the operation is the same as above.

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ​​of the same keys, making the program design more flexible. array_merge

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ​​of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

How to correctly set the PATH environment variable in Linux How to correctly set the PATH environment variable in Linux Feb 22, 2024 pm 08:57 PM

How to correctly set the PATH environment variable in Linux In the Linux operating system, environment variables are one of the important mechanisms used to store system-level configuration information. Among them, the PATH environment variable is used to specify the directories in which the system searches for executable files. Correctly setting the PATH environment variable is a key step to ensure the normal operation of the system. This article will introduce how to correctly set the PATH environment variable in Linux and provide specific code examples. 1. Check the current PATH environment variable and enter the following command in the terminal

How to configure path environment variable in java How to configure path environment variable in java Nov 15, 2023 pm 01:20 PM

Configuration steps: 1. Find the Java installation directory; 2. Find the system environment variable settings; 3. In the environment variable window, find the variable named "Path" and click the edit button; 4. In the pop-up edit environment variable window , click the "New" button, and enter the Java installation path in the pop-up dialog box; 5. After confirming that the input is correct, click the "OK" button.

The role and importance of the PATH environment variable in Linux The role and importance of the PATH environment variable in Linux Feb 21, 2024 pm 02:09 PM

"The Role and Importance of the PATH Environment Variable in Linux" The PATH environment variable is one of the very important environment variables in the Linux system. It defines which directories the system searches for executable programs. In the Linux system, when the user enters a command in the terminal, the system will search one by one in the directories listed in the PATH environment variable to see if the executable file of the command exists. If found, it will be executed. Otherwise, "commandnotfound" will be prompted. The role of the PATH environment variable: Simplified

See all articles