首頁 > 後端開發 > php教程 > 顯示所有檔案和資料夾 php檔案及資料夾操作(建立、刪除、移動、複製)

顯示所有檔案和資料夾 php檔案及資料夾操作(建立、刪除、移動、複製)

WBOY
發布: 2016-07-28 08:26:50
原創
1113 人瀏覽過

建立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');
登入後複製

另一種調用方式:  推薦一個自己業餘時間開發的網盤搜尋引擎,360盤搜索(www.360panso.com) 以上就介紹了顯示所有檔案和資料夾 php檔案及資料夾操作(建立、刪除、移動、複製),包括了顯示所有檔案和資料夾方面的內容,希望對PHP教學有興趣的朋友有所幫助。
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板