>  기사  >  백엔드 개발  >  모든 파일 및 폴더 표시 PHP 파일 및 폴더 작업(생성, 삭제, 이동, 복사)

모든 파일 및 폴더 표시 PHP 파일 및 폴더 작업(생성, 삭제, 이동, 복사)

WBOY
WBOY원래의
2016-07-28 08:26:501085검색

다음 내용과 호출 방법을 사용하여 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>?>

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

또 다른 통화 방법 :

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

틈날 때 개발한 네트워크 디스크 검색 엔진 360panso(www.360panso.com)를 추천합니다 🎜>

위 내용은 모든 파일과 폴더를 표시하는 내용을 포함하여 PHP 파일 및 폴더 작업(생성, 삭제, 이동, 복사)을 표시하는 방법을 소개하고 있으며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.