显示所有文件和文件夹 php文件及文件夹操作(创建、删除、移动、复制)

WBOY
Freigeben: 2016-07-28 08:26:50
Original
1089 Leute haben es durchsucht

创建FileUtil.php文件,内容及调用方式如下:

 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>?>
Nach dem Login kopieren

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

另一种调用方式:

<span>$fu</span> = <span>new</span><span> FileUtil();
</span><span>$fu</span>->copyFile('a/1/2/3', 'a/1/2/4');
Nach dem Login kopieren

  推荐一个自己业余时间开发的网盘搜索引擎,360盘搜(www.360panso.com)

以上就介绍了显示所有文件和文件夹 php文件及文件夹操作(创建、删除、移动、复制),包括了显示所有文件和文件夹方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage