Home > Backend Development > PHP Tutorial > Display all files and folders PHP file and folder operations (create, delete, move, copy)

Display all files and folders PHP file and folder operations (create, delete, move, copy)

WBOY
Release: 2016-07-28 08:26:50
Original
1111 people have browsed it

Create the FileUtil.php file, the content and calling method are as follows:

 php文件及文件夹操作(创建、删除、移动、复制)

<?<span>php

</span><span>/*</span><span>*
 * 操纵文件类
 * 
 * 例子:
 * FileUtil::createDir('a/1/2/3');                    测试建立文件夹 建一个a/1/2/3文件夹
 * FileUtil::createFile('b/1/2/3');                    测试建立文件        在b/1/2/文件夹下面建一个3文件
 * FileUtil::createFile('b/1/2/3.exe');             测试建立文件        在b/1/2/文件夹下面建一个3.exe文件
 * FileUtil::copyDir('b','d/e');                    测试复制文件夹 建立一个d/e文件夹,把b文件夹下的内容复制进去
 * FileUtil::copyFile('b/1/2/3.exe','b/b/3.exe'); 测试复制文件        建立一个b/b文件夹,并把b/1/2文件夹中的3.exe文件复制进去
 * FileUtil::moveDir('a/','b/c');                    测试移动文件夹 建立一个b/c文件夹,并把a文件夹下的内容移动进去,并删除a文件夹
 * FileUtil::moveFile('b/1/2/3.exe','b/d/3.exe'); 测试移动文件        建立一个b/d文件夹,并把b/1/2中的3.exe移动进去                   
 * FileUtil::unlinkFile('b/d/3.exe');             测试删除文件        删除b/d/3.exe文件
 * FileUtil::unlinkDir('d');                      测试删除文件夹 删除d文件夹
 </span><span>*/</span><span>class</span><span> FileUtil {

    </span><span>/*</span><span>*
     * 建立文件夹
     *
     * @param string $aimUrl
     * @return viod
     </span><span>*/</span><span>function</span> createDir(<span>$aimUrl</span><span>) {
        </span><span>$aimUrl</span> = <span>str_replace</span>('', '/', <span>$aimUrl</span><span>);
        </span><span>$aimDir</span> = ''<span>;
        </span><span>$arr</span> = <span>explode</span>('/', <span>$aimUrl</span><span>);
        </span><span>$result</span> = <span>true</span><span>;
        </span><span>foreach</span> (<span>$arr</span><span>as</span><span>$str</span><span>) {
            </span><span>$aimDir</span> .= <span>$str</span> . '/'<span>;
            </span><span>if</span> (!<span>file_exists</span>(<span>$aimDir</span><span>)) {
                </span><span>$result</span> = <span>mkdir</span>(<span>$aimDir</span><span>);
            }
        }
        </span><span>return</span><span>$result</span><span>;
    }

    </span><span>/*</span><span>*
     * 建立文件
     *
     * @param string $aimUrl 
     * @param boolean $overWrite 该参数控制是否覆盖原文件
     * @return boolean
     </span><span>*/</span><span>function</span> createFile(<span>$aimUrl</span>, <span>$overWrite</span> = <span>false</span><span>) {
        </span><span>if</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> == <span>false</span><span>) {
            </span><span>return</span><span>false</span><span>;
        } </span><span>elseif</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> == <span>true</span><span>) {
            FileUtil </span>:: unlinkFile(<span>$aimUrl</span><span>);
        }
        </span><span>$aimDir</span> = <span>dirname</span>(<span>$aimUrl</span><span>);
        FileUtil </span>:: createDir(<span>$aimDir</span><span>);
        </span><span>touch</span>(<span>$aimUrl</span><span>);
        </span><span>return</span><span>true</span><span>;
    }

    </span><span>/*</span><span>*
     * 移动文件夹
     *
     * @param string $oldDir
     * @param string $aimDir
     * @param boolean $overWrite 该参数控制是否覆盖原文件
     * @return boolean
     </span><span>*/</span><span>function</span> moveDir(<span>$oldDir</span>, <span>$aimDir</span>, <span>$overWrite</span> = <span>false</span><span>) {
        </span><span>$aimDir</span> = <span>str_replace</span>('', '/', <span>$aimDir</span><span>);
        </span><span>$aimDir</span> = <span>substr</span>(<span>$aimDir</span>, -1) == '/' ? <span>$aimDir</span> : <span>$aimDir</span> . '/'<span>;
        </span><span>$oldDir</span> = <span>str_replace</span>('', '/', <span>$oldDir</span><span>);
        </span><span>$oldDir</span> = <span>substr</span>(<span>$oldDir</span>, -1) == '/' ? <span>$oldDir</span> : <span>$oldDir</span> . '/'<span>;
        </span><span>if</span> (!<span>is_dir</span>(<span>$oldDir</span><span>)) {
            </span><span>return</span><span>false</span><span>;
        }
        </span><span>if</span> (!<span>file_exists</span>(<span>$aimDir</span><span>)) {
            FileUtil </span>:: createDir(<span>$aimDir</span><span>);
        }
        @ </span><span>$dirHandle</span> = <span>opendir</span>(<span>$oldDir</span><span>);
        </span><span>if</span> (!<span>$dirHandle</span><span>) {
            </span><span>return</span><span>false</span><span>;
        }
        </span><span>while</span> (<span>false</span> !== (<span>$file</span> = <span>readdir</span>(<span>$dirHandle</span><span>))) {
            </span><span>if</span> (<span>$file</span> == '.' || <span>$file</span> == '..'<span>) {
                </span><span>continue</span><span>;
            }
            </span><span>if</span> (!<span>is_dir</span>(<span>$oldDir</span> . <span>$file</span><span>)) {
                FileUtil </span>:: moveFile(<span>$oldDir</span> . <span>$file</span>, <span>$aimDir</span> . <span>$file</span>, <span>$overWrite</span><span>);
            } </span><span>else</span><span> {
                FileUtil </span>:: moveDir(<span>$oldDir</span> . <span>$file</span>, <span>$aimDir</span> . <span>$file</span>, <span>$overWrite</span><span>);
            }
        }
        </span><span>closedir</span>(<span>$dirHandle</span><span>);
        </span><span>return</span><span>rmdir</span>(<span>$oldDir</span><span>);
    }

    </span><span>/*</span><span>*
     * 移动文件
     *
     * @param string $fileUrl
     * @param string $aimUrl
     * @param boolean $overWrite 该参数控制是否覆盖原文件
     * @return boolean
     </span><span>*/</span><span>function</span> moveFile(<span>$fileUrl</span>, <span>$aimUrl</span>, <span>$overWrite</span> = <span>false</span><span>) {
        </span><span>if</span> (!<span>file_exists</span>(<span>$fileUrl</span><span>)) {
            </span><span>return</span><span>false</span><span>;
        }
        </span><span>if</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> = <span>false</span><span>) {
            </span><span>return</span><span>false</span><span>;
        } </span><span>elseif</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> = <span>true</span><span>) {
            FileUtil </span>:: unlinkFile(<span>$aimUrl</span><span>);
        }
        </span><span>$aimDir</span> = <span>dirname</span>(<span>$aimUrl</span><span>);
        FileUtil </span>:: createDir(<span>$aimDir</span><span>);
        </span><span>rename</span>(<span>$fileUrl</span>, <span>$aimUrl</span><span>);
        </span><span>return</span><span>true</span><span>;
    }

    </span><span>/*</span><span>*
     * 删除文件夹
     *
     * @param string $aimDir
     * @return boolean
     </span><span>*/</span><span>function</span> unlinkDir(<span>$aimDir</span><span>) {
        </span><span>$aimDir</span> = <span>str_replace</span>('', '/', <span>$aimDir</span><span>);
        </span><span>$aimDir</span> = <span>substr</span>(<span>$aimDir</span>, -1) == '/' ? <span>$aimDir</span> : <span>$aimDir</span> . '/'<span>;
        </span><span>if</span> (!<span>is_dir</span>(<span>$aimDir</span><span>)) {
            </span><span>return</span><span>false</span><span>;
        }
        </span><span>$dirHandle</span> = <span>opendir</span>(<span>$aimDir</span><span>);
        </span><span>while</span> (<span>false</span> !== (<span>$file</span> = <span>readdir</span>(<span>$dirHandle</span><span>))) {
            </span><span>if</span> (<span>$file</span> == '.' || <span>$file</span> == '..'<span>) {
                </span><span>continue</span><span>;
            }
            </span><span>if</span> (!<span>is_dir</span>(<span>$aimDir</span> . <span>$file</span><span>)) {
                FileUtil </span>:: unlinkFile(<span>$aimDir</span> . <span>$file</span><span>);
            } </span><span>else</span><span> {
                FileUtil </span>:: unlinkDir(<span>$aimDir</span> . <span>$file</span><span>);
            }
        }
        </span><span>closedir</span>(<span>$dirHandle</span><span>);
        </span><span>return</span><span>rmdir</span>(<span>$aimDir</span><span>);
    }

    </span><span>/*</span><span>*
     * 删除文件
     *
     * @param string $aimUrl
     * @return boolean
     </span><span>*/</span><span>function</span> unlinkFile(<span>$aimUrl</span><span>) {
        </span><span>if</span> (<span>file_exists</span>(<span>$aimUrl</span><span>)) {
            </span><span>unlink</span>(<span>$aimUrl</span><span>);
            </span><span>return</span><span>true</span><span>;
        } </span><span>else</span><span> {
            </span><span>return</span><span>false</span><span>;
        }
    }

    </span><span>/*</span><span>*
     * 复制文件夹
     *
     * @param string $oldDir
     * @param string $aimDir
     * @param boolean $overWrite 该参数控制是否覆盖原文件
     * @return boolean
     </span><span>*/</span><span>function</span> copyDir(<span>$oldDir</span>, <span>$aimDir</span>, <span>$overWrite</span> = <span>false</span><span>) {
        </span><span>$aimDir</span> = <span>str_replace</span>('', '/', <span>$aimDir</span><span>);
        </span><span>$aimDir</span> = <span>substr</span>(<span>$aimDir</span>, -1) == '/' ? <span>$aimDir</span> : <span>$aimDir</span> . '/'<span>;
        </span><span>$oldDir</span> = <span>str_replace</span>('', '/', <span>$oldDir</span><span>);
        </span><span>$oldDir</span> = <span>substr</span>(<span>$oldDir</span>, -1) == '/' ? <span>$oldDir</span> : <span>$oldDir</span> . '/'<span>;
        </span><span>if</span> (!<span>is_dir</span>(<span>$oldDir</span><span>)) {
            </span><span>return</span><span>false</span><span>;
        }
        </span><span>if</span> (!<span>file_exists</span>(<span>$aimDir</span><span>)) {
            FileUtil </span>:: createDir(<span>$aimDir</span><span>);
        }
        </span><span>$dirHandle</span> = <span>opendir</span>(<span>$oldDir</span><span>);
        </span><span>while</span> (<span>false</span> !== (<span>$file</span> = <span>readdir</span>(<span>$dirHandle</span><span>))) {
            </span><span>if</span> (<span>$file</span> == '.' || <span>$file</span> == '..'<span>) {
                </span><span>continue</span><span>;
            }
            </span><span>if</span> (!<span>is_dir</span>(<span>$oldDir</span> . <span>$file</span><span>)) {
                FileUtil </span>:: copyFile(<span>$oldDir</span> . <span>$file</span>, <span>$aimDir</span> . <span>$file</span>, <span>$overWrite</span><span>);
            } </span><span>else</span><span> {
                FileUtil </span>:: copyDir(<span>$oldDir</span> . <span>$file</span>, <span>$aimDir</span> . <span>$file</span>, <span>$overWrite</span><span>);
            }
        }
        </span><span>return</span><span>closedir</span>(<span>$dirHandle</span><span>);
    }

    </span><span>/*</span><span>*
     * 复制文件
     *
     * @param string $fileUrl
     * @param string $aimUrl
     * @param boolean $overWrite 该参数控制是否覆盖原文件
     * @return boolean
     </span><span>*/</span><span>function</span> copyFile(<span>$fileUrl</span>, <span>$aimUrl</span>, <span>$overWrite</span> = <span>false</span><span>) {
        </span><span>if</span> (!<span>file_exists</span>(<span>$fileUrl</span><span>)) {
            </span><span>return</span><span>false</span><span>;
        }
        </span><span>if</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> == <span>false</span><span>) {
            </span><span>return</span><span>false</span><span>;
        } </span><span>elseif</span> (<span>file_exists</span>(<span>$aimUrl</span>) && <span>$overWrite</span> == <span>true</span><span>) {
            FileUtil </span>:: unlinkFile(<span>$aimUrl</span><span>);
        }
        </span><span>$aimDir</span> = <span>dirname</span>(<span>$aimUrl</span><span>);
        FileUtil </span>:: createDir(<span>$aimDir</span><span>);
        </span><span>copy</span>(<span>$fileUrl</span>, <span>$aimUrl</span><span>);
        </span><span>return</span><span>true</span><span>;
    }

}

</span>?>
Copy after login

 php文件及文件夹操作(创建、删除、移动、复制)

Another calling method:

<span>$fu</span> = <span>new</span><span> FileUtil();
</span><span>$fu</span>->copyFile('a/1/2/3', 'a/1/2/4');
Copy after login

Recommend a network disk search engine developed in your spare time. 360 Panso (www.360panso.com)

The above introduces how to display all files and folders PHP file and folder operations (create, delete, move, copy), including the content of displaying all files and folders. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template